In this lesson, we will talk about the path in Linux, which is one of the essential concepts in Linux administration.
What is a Path?
Every file and directory has a unique location in the Linux file system structure. To access files and directories or to call them in a command or script, Administrators need Path. A Path is an address to files and directories location. There are two ways to address a file or directory. Using absolute path or relative path.
Absolute path and Relative path
The Linux file system Hierarchy standard starts from the root directory “/,” as we covered FHS. Therefore, any path that begins with the “/” is an absolute path, and other paths are relative.
As the image above shows, to get access to the “script.sh” file in the “testdir” directory, we showed two paths :
The absolute path: /home/sara/testdir/script.sh
The relative path: ./testdir/script.sh or testdir/script.sh
Note: The first “/” in absolute paths refer to the root directory, and the rest of the slashes are just delimiters. Most Linux beginners often get confused about slashes.
Absolute path
Absolute paths are related to the root “/” directory. They are not dependent on the current working directory. Therefore absolute paths must start from the root “/” directory. They contain the complete path from the root directory to the target file or directory.
Relative path
Relative paths are entirely dependent on the current working directory. Therefore administrators must send files or directory paths to commands relative to the current working directory. The current working directory is the “sara” directory in the example above.
First, we introduce some commands to understand paths better and do the practices.
Directory content list commands
There are various commands and tools to display the content of directories in Linux. Here are the three frequently used directory content list commands:
- ls
- dir
- tree
Consider the image below as the primary example. We will describe and practice commands based on this image.
ls command
The “ls” command is one of every Linux distribution’s most frequently used commands. By default, the “ls” command displays a list of current working directory contents. Also, the “ls” command can list other directories’ content if the user sends the directory path as the argument to the ls command. This command covers all types of content in the directory; therefore, it also Lists information about the FILEs.
Note : By default “ls” command Sorts entries alphabetically .
ls command syntax
ls [OPTION]… [File Or Directory PATH]…
ls command examples
Without switches and path: Displays content of the current working directory.
Without switches and with file path: If the argument is a directory path, it shows the content, and if it’s a file path, it displays the information of the file.
[sara@Lpic1-CentOs9 ~]$ pwd
/home/sara
[sara@Lpic1-CentOs9 ~]$ ls
Desktop docs Documents Downloads img Music Pictures Public Templates Videos
************************Absolute Path********************
[sara@Lpic1-CentOs9 ~]$ ls /home/sara/img/
pic1.jpg
************************Relative Path********************
[sara@Lpic1-CentOs9 ~]$ ls ./img/
pic1.jpg
[sara@Lpic1-CentOs9 ~]$ ls img
pic1.jpg
The current working directory is “/home/sara” in this example. Therefore the “ls” command displays the contents of “/home/sara.” In the second example, we gave a directory path (based on the image above), and the “ls” command displayed the content of that directory.
Note:Note: In the second example, firstly, we used the absolute path. To write this command with a relative path, As shown in the example code above, consider the current working directory as a base and write the path from the base.
Current working directoy : “/home/sara” write the path from this base.
Relative path : img/ or ./img
Note: we can use dot and double dots and tilda in the relative paths as below:
Single Dot : The current working directory
Double Dots : The Parent working directory
The tilda : The current user home directory
Practice
A- Suppose that the current working directory is “/opt/tomcat” write two ls commands to display the content of the logs directory using an absolute and relative command.
B- With the previous suppose, write ls command that displays docs directory in /home/sara using a relative path.
****************A********************
[sara@Lpic1-CentOs9 ~]$ pwd
/opt/tomcat
[sara@Lpic1-CentOs9 ~]$ ls /opt/tomcat/logs
catalina.2022-05-11.log catalina.out localhost.2022-05-11.log localhost_access_log.2022-05-11.txt
[sara@Lpic1-CentOs9 ~]$ ls logs/
catalina.2022-05-11.log catalina.out localhost.2022-05-11.log localhost_access_log.2022-05-11.txt
[sara@Lpic1-CentOs9 ~]$ ls ./logs
catalina.2022-05-11.log catalina.out localhost.2022-05-11.log localhost_access_log.2022-05-11.txt
****************B********************
[sara@Lpic1-CentOs9 ~]$ pwd
/opt/tomcat
[sara@Lpic1-CentOs9 ~]$ ls ~/docs
plan.txt
-a,–all
Hidden files or dotfiles are service script files or service configuration files that the regular “ls” command doesn’t display for security reasons, such as the “.bashrc” file that contains the user initialization script. Therefore to show all content of the directory, including dotfiles, use –a or –all switch.
[sara@Lpic1-CentOs9 ~]$ ls -a
. .cache Documents .java .netbeans .ssh .Xauthority .xorgxrdp.12.log.old
.. .config Downloads .local .pcsc12 Templates .xorgxrdp.10.log .xsession-errors
.bash_history CyberoamLinuxClient .gnupg .mozilla Pictures thinclient_drives .xorgxrdp.11.log
.bash_logout Desktop .ICEauthority Music .profile Videos .xorgxrdp.11.log.old
.bashrc docs img .nbi Public .vnc .xorgxrdp.12.log
-l
“ls” command with the “-l” switch displays the content in long format. It contains valuable information along with the filename. This information contain columns below:
Column 1: Displays the file permission ( we will cover this later)
Column 2: Displays the number of links to the file
Columns 3,4: Displays the owner and the owner group of the file
Column 5: Displays the size of the file
Columns 6 to 8: Displays the last modified date and time
Column 9: Displays the file or folder name
[sara@Lpic1-CentOs9 ~]$ ls -l
drwxr-xr-x 2 sara sara 4096 Dec 24 2022 Desktop
drwxr-xr-x 2 sara sara 4096 May 14 17:07 docs
drwxr-xr-x 3 sara sara 4096 Dec 24 2022 Documents
drwxr-xr-x 2 sara sara 4096 Dec 19 2022 Downloads
drwxr-xr-x 2 sara sara 4096 May 14 14:59 img
drwxr-xr-x 2 sara sara 4096 Dec 19 2022 Music
drwxr-xr-x 2 sara sara 4096 Dec 19 2022 Pictures
drwxr-xr-x 2 sara sara 4096 Dec 19 2022 Public
drwxr-xr-x 2 sara sara 4096 Dec 19 2022 Templates
How the first column defines the permission?
The first column contains ten bits of data. As the image below.
The first bit displays the type of file. As we mentioned before, everything in Linux is a file. The essential Linux file types are :
“-”: regular file
“d”: directory
“l”: symbolic link
Linux users usually consider this type of file as a shortcut. It means access to these files from a different location and another file name. There are two types of symbolic links, hard links, and soft links. We will describe links in later lessons.
“b”: block devices files
“c”: character device files
Linux identifies all hardware devices as special files such as printers, hard drives, and even terminal emulators. Therefore applications can access and utilize files and devices in the same way, and this makes developing programs easier. All of the special files are in the “/dev” directory, including two types of special files: character device files and block device files.
Character device files represent devices that transfer data in bytes, which means the data has a format, such as terminal emulators. Run “ls -lah /dev/pts/” and see the output. Block device files represent devices that transfer data in blocks with no format, such as hard disks.
[root@Lpic1-CentOs9 ~]# ls -l /dev/pts/
total 0
crw--w----. 1 root tty 136, 0 May 14 2022 0
c---------. 1 root root 5, 2 May 9 11:38 ptmx
[root@Lpic1-CentOs9 ~]# ls -lah /dev/sd*
brw-rw----. 1 root disk 8, 0 May 9 11:38 /dev/sda
brw-rw----. 1 root disk 8, 1 May 9 11:38 /dev/sda1
brw-rw----. 1 root disk 8, 2 May 9 11:38 /dev/sda2
Permission
After the file type, the nine bites define the file or directory permission. Permission groups are the owner and the group assigned to the file, and the other users.
The three bits define the Read access “r” and write access “w,” and execute access “x.” We will cover this part in future lessons.
-h , –human-readable
When we run the “ls –l” command, it displays the size of files in terms of bytes which is so hard to read. The “–h” or “–human-readable” displays the size of files in a human-readable format such as Mb, Gb, Tb.
[sara@Lpic1-CentOs9 ~]$ ls -lh
drwxr-xr-x 2 sara sara 4.0K Dec 24 2022 Desktop
drwxr-xr-x 2 sara sara 4.0K May 14 17:07 docs
drwxr-xr-x 3 sara sara 4.0K Dec 24 2022 Documents
drwxr-xr-x 2 sara sara 4.0K Dec 19 2022 Downloads
drwxr-xr-x 2 sara sara 4.0K May 14 14:59 img
drwxr-xr-x 2 sara sara 4.0K Dec 19 2022 Music
drwxr-xr-x 2 sara sara 4.0K Dec 19 2022 Pictures
drwxr-xr-x 2 sara sara 4.0K Dec 19 2022 Public
drwxr-xr-x 2 sara sara 4.0K Dec 19 2022 Templates
Note: switch combination is widespread in Linux. As the example above shows, we can use a combination of switches such as “ls -lah.”