linux Part 6
Introduction to Linux File System
Linux was designed so that multiple users can connect to the system using different terminals, while all their personal information and desktop configuration are stored separately. Amongst all the users that use the machine, Linux differentiates between 2 different users
Normal user
Super user /administrator (root users)
Superusers can access all parts of the system and execute admin tasks.
All users including the super user have their home directory, this is where they store all their private data including documents, emails and bookmarks and photos. In addition to this there are system directories which hold system configuration files and execution files, however these can only be modified by the administrator. Any user can decide if they want to access their files via a GUI or through the command line. The traditional way to do this is through the command line, this is often faster but does require deep knowledge of their commands and Syntax, so that you can list files, create files, delete files and edit files and look at their properties.
All Linux files are located in a tree like structure
/ <<<<<<<<<<< this is the root not to be confused with the root of the super user, in windows this would be the C drive
/bin
/sbin
/dev
/etc
/home
/root
In Linux the path structure uses forward slashes
/home/username/documents
Linux File System Key Features
Unlike windows and Linux devices drives and partitions are not visible unless they have been mounted, that means they have to be integrated in the file system in a specific location in the directory structure.
A normal user can not data on a partition or device unless it has been mounted. Most of the time users do not have to mount partitions or devices manually though
During the installation of Linux, users can define partitions, that will be automatically mounted when ever your system is started. When you are dealing with removable devices like a USB sticks or external hard drives, these are normally detected automatically and mounted by your system. Desktop environments such as gnome and kde will notify you of a new device.
Linux will distinguish between lowercase and uppercase in the file system. So name files as
Test.txt
tesT.txt
tEst.txt
The 3 files are accounted as 3 different files
The same applies to folders if you have 3 folders
Folder1
FOlder1
foLder1
They are all classed as 3 different folders
If you deal with windows files have an extension. Jpg, png, rar, doc etc. With Linux there is no file extension needed. If you have a photo with no extension it still will open in a photo viewing program.
Similar to windows, Linux does distinguish between normal files and hidden files. In Linux hidden files have a “.” before the file name. For example
./config
The types of files that are generally hidden are configuration files.To be able to see the hidden files you must switch the view in your file manager or use certain commands in your shell
Because Linux is a multi user system every file in a Linux file system is going to belong to some user and some group. Only the owner of a file or a directory can grant other users access to that file, however there is one exception, the root user has essentially “god mode” they can do whatever they want to files.
Linux distinguishes 3 types of of permissions.
Read
Write
Execute
For users to be able to access a file or folder they need to have at least read permission. To change permissions you can use the shell or a file manager.
FHS File system Hierarchy Standard Distinguishes between shareable and un-shareable files, and between static and variable files.
Shareable files
This will be reasonably shared between different computers, such as user-data files, program binary files and things like that
Un-shareable files
These files are going to be related to system specific files, like system configuration files
Static files and Variable files
Program executable’s are good examples of static files
Variable files are files that can be changed by users, automated scripts or servers. For example any of the files you store in a user directory like documents and photos these files are going to be variable
Navigating Files and Directories
To manipulate files it’s helpful to know what they are , if you use the ls command ls with options
ls -a it shows you all files including hidden
ls -l will show you permissions on the file, ownership file sizes and file creation dates
For more options just use the man ls command
cd which is short for change directory. Linux uses the forward slash for the directory indicateror
Remember backslash serves as a quote or a escape character
Some Shells do not show you what directory you are in, simply use the pwd “present working directory” to find what directory you are in.
Linux uses a unified directory tree, this means all the files can be found under the root directory /
Files and directories can be referenced in different ways
Absolute references
Home references
Relative references
Absolute
These files are relative to the root directory. For example if use: /Home/user1/file1.txt
The file1.txt is in the home directory which is relative to the root drive. Such references always begin with a slash.
Home directory
a tilde (~) character the paths home directory
~/file1.txt
Relative
These are file references relative to the current directory you are in, every linux directory contains to special directories, one is known as
dot dot (..) refers to your parent directory – the directory, the one that is one level higher
dot (.) refers to this directory ( current directory )
These are hidden files if you run the ls -a or ls –all command you will be to see them
For example imagine your dir1 located /home/user1/dir1 this is the (.) dot (current directory)
Then you wish to go back into the parent directory and open a file, you can type ../dir2/afile.txt
File creation and Management
touch – creates files
Example touch file.txt creates a new file
Creating an empty file can act as a scratch file
cp is used for copying files
You can use it to copy a source file to a destination file. From the command line the source file and be renamed to a different file name
Example cp ./home/Downloads/video.mp4 /.home/Documents/videocopy.mp4
Type man cp for more switch options
Move and rename files
You can also move a file and rename it at the same time
Example mv ./home/Downloads/photo.jpg /.home/Documents/newphoto.jpg
Renaming a file in the same directory ./home/Downloads/photo.jpg /.home/Documents/newphoto.jpg
Creating Links / Symbolic Links
In Linux you can make several links point to one file, these are known as hard links or Symbolic links, these links are created using the ln (LN) / the link command.
The Hard Link
The hard link is a duplicate directory entry where both entries point to the same file. Because they both work together tying this low level file system data structures, hard links and only exist on a single files system.
In a hard link scenario neither filename is going to be a priority over another, both of them are going to be tied to the files data structures.
How to create a Hard link
ln originalname linkname
Symbolic links
A file that refers to another file by name, this is known as a soft link or a symlink. When you tell a program to read or write from another program, linux is just going to redirect the access back to the original file.
Because file names work by symbolic references this can cross file-system boundaries, so if you type in:
ln -s originalname linkname
This will create a soft-link /sym-link (Symbolic Link), these links refer you back to the original file, so when you change the symbolic link you are making changes to the original file too.
Wild cards and Case Sensitivity
Linux is dependent on the case sensitivity of characters. When you use a wild card, iots a symbol that stands in for other characters a user may want.
Wild cards can be used with files and sometimes it’s called globbing, 3 classes of wildcards are common to linux. The first is the “?” For example if you use:
b??l
This could match a couple of words – bowl ball bull or any other 4 word character that starts with a b and ends with an l
The next wild card we can use is an asterix “*”, using an asterix well match any character it will match anything that has a B and L with anything between or no
b*l – bowl bull b99l bummel brawl bl
The 3rd type of wildcard we can use is a bracketed value [ ] for example:
B[ao][lw]l this will find for example, bowl, ball, but it wont find bull, because there is know you in the first set of brackets.
How to use Wild cards
ls b??l
Be careful when copying files using wildcards, let’s say if you are copying 2 files with wildcards, if you forget to put the destination directory where you are copying to it may be that you overwrite the file in the same directory … study this and replicate error to understand this
Linux case sensitivity
Afile.txt
Afile.txt
AFILE.txt
All 3 files are classed in linux as different files. In windows files are not treat as case sensitive whether its upper case, lowercase or mixed.
Case sensitivity is a function of the linux file system and not the operating system itself. If I connect a usb drive formatted on a windows machine using fat32 and connect it to a linux system that case sensitivity would not exist, this is particularly important so you can access NTFS and FAT file systems because they are windows systems that are case insensitive.
Important – Bash (shell) will still regard the windows file systems with case sensitivity. Its best to treat everything in Linux with Case sensitivity.
Manipulating Directories
Think of Directories as folders because most file managers are going to represent a directory by a file folder icon
Directory commands
mkdir – Creates a new directory
man mkdir
rmdir – this will remove a directory – this can only delete empty directories
rmdir -p this will deleted folders within folders within folders – once again it will only delete empty folders
If you want to delete all files and folders with in a folder
rm -r “ -r stands for recursive. Example rm -r musicfiles
Copying directories and its contents
cp -r or a
mv can be used to move files.
ln -s can create a symbolic link to a directory
Manipulating files and directories
Scenario Documents folder
Folder1 Folder2 bale.txt ball.txt. bowl.txt bull.txt
Create all the files using shell
mkdir Folder1
mkdir Folder2
touch bale.txt
touch ball.txt
touch bowl.txt
touch bull.txt
Note I have created file extensions but they do not need to be there, linux recognizes files without their extensions as linux actually looks at what is in the file, you can take away the file extension it will still be recognized as text file.
Hiding files
If we go into the GUI of ubuntu then /home/user/Documents we will see the 2 folders and 4 files that were created.
If right click on one of the files copy and paste it and then rename it, for example .bale1.txt
Notice “.” dot or period before the file name, this will make the file hidden. Once the period has been applied to the filename the file will disappear.
Now enter your shell and navigate to the Documents folder
Now run ls as you will see or rather will not see the .bale1.txt
Now run ls -la and it will list all files including hidden files in the directory .
ls -la list long all files
Now you can see the hidden file.
Moving files into into folders using the move command
mv bull.txt Folder2/bull.txt – this is an opportunity if we wanted to change the text file name from bull.txt to something else we could do it now, but we will just leave it for now.
Once the file has been moved navigate to you Folder2 and list the contents, you should see bull.txt and bowl.txt
You should have 2 txt files still in your Documents folder, bale.txt and ball.txt, we will move both in one command to folder1 using wildcards
mv ba??.txt Folder1
or another option would be if you wanted to move specific files, for example with the txt.extension
mv *.txt Folder1
Moving the hidden txt file from the Documents folder. Note when we move the txt files in the previous command mv *.txt the hidden files are not moved because the are hidden. To move this file we will move it as follows
mv .bale1.txt Folder1
Listing other directories while currently in another
Say we are currently in this dir /Documents/Folder1 and we want to list the continents in another folder
We would do this by simply using the ls and then the path to the directory, so if we wanted to list Folder2 without switching directories.
~/Documents/Folder1$ ls /home/rwelsh/Documents/Folder2
bowl.txt bull.txt
Another way would be to do it relative to the home directory using the tilde (~)
ls ~/Documents/Folder2
bowl.txt bull.txt
Another way to do it is to use the ls ../Documents/Folder2
bowl.txt bull.txt
Moving files in from another directory while in present working directory (pwd) using the relative way of doing. So we are currently in Folder1, this means we would use.
mv ../Folder2/b??l.txt then one of 3 ways ~/Documents/Folder1
/home/user/Documents/Folder1
../Folder2
You can also rename files by using the mv command, for example we will rename a file in the same directory
mv ball.txt bat.txt
You can also use cp (copy) in exactly the same way except copy is going to leave the original files in the location that they were copied from
Wildcards
[ ] ? *
Examples
We want to search for ball bowl in a directory
ls -la b[u][owl]l.txt
This command will look for files that begin with b, looks for files that have a second letter u , and a third letter with a o, w or l, then with a l at the end and with .txt.
Result is that it found in the directory bull.txt
-rw-rw-r– 1 rwelsh rwelsh 0 Sep 22 12:25 bull.txt
*** Section six “ sub section 35” master globbing As you get better move up to system admin you will need this more and more, so learning it is like second nature. Its used for searching certain users, ip addresses, and accounts.
Directories
Create a directory in Documents. Call it Folder3 and rename it to Robert
mv Folder3 Robert
Remove a empty Directory
rmdir Robert
Remove multiple txt files from a directory
rm *.txt
Remove complete directory with files in it including hidden files
rm -r (recursive) Folder – this will remove the Folder and anything inside of it.
.