This tutorial explains what the absolute path and the relative path names are in Linux with examples. Learn the differences between the absolute path and the relative path along with the meaning of single dot and double dots in pathname.
Linux file system is built from files and directories. Files are used to store the data. Directories are used to organize the files systematically. The directory root (/) is the main directory in Linux. All directories and files are created and managed under this directory. The location of a file or directory from this directory is known as the path of that file or directory. A path, based on how it is written, can be categorized in two types; absolute path and relative path.
Absolute path starts from the directory root (/) and goes up to the actual object (file or directory). It contains the names of all directories that come in the middle of the directory root and the actual object. In this, name of the parent directory is written in the left.
Let’s take an example, suppose a user named sanjay creates a directory named test in his home directory. What will the absolute path of this directory?
To write the absolute path of this directory, we have to start writing the path from the directory root. The directory root is written as / (forward slash).
After / (root directory), we have to write the name of the directory in which user’s home directory is located.
By default, Linux places user’s home directory in the directory named home. Usually, this directory is created just under the directory root (/).
If we write the name of the home directory just after the root directory, we get the absolute path of the home directory.
If we write the name of the user’s home directory just after the absolute path of the home directory, we get the absolute path of the user’s home directory.
By default, Linux uses user’s username to create user’s home directory. In this example, username is sanjay, hence the name of its home directory is also sanjay.
Following the same track, if we write the name of the directory or file which is created in user’s home directory just after the
absolute path of the user’s home directory, we get the absolute path of that directory or file.
In this example, absolute path of the directory test will be; /root/home/sanjay/test.
- First forward slash (/) in the absolute path represents the directory root. Besides this, all slashes in the path represent the directory separator.
- Besides the last name, all names in the absolute path belong to directories. Last name can belong to file or directory.
- In the absolute path, directories names are written in their hierarchy order. Parent directory’s name is written in the left side.
- Absolute path does not change when we change the current directory.
- To know to the absolute path of the current directory, we can use the command pwd.
Relative path starts from the current directory and goes up to the actual object. Relative path depends on the current directory. When we change the directory, relative path also changes. Just like the absolute path, the name of the parent directory is written in the left side. Unlike the absolute path, all slashes in the relative path represent the directory separator.
Before we take the examples of the relative path, let’s understand the special meanings of single dot and double dots used in the relative path.
Single dot (.) and double dots (..) in Linux
In Linux every directory contains two dots; single dot and double dots. When a directory is created,
both the single dot and the double dots are also automatically created in it. By default these dots are hidden and do not show in the output of the command ls. To view these dots, we have to use the option a with the command ls.
The single dot refers to the directory itself and the double dots refers to its parent directory or the directory
that contains it. Shell allows us to access the current directory and the parent directory by using the single dot and the double dots respectively.
Relative path also uses these dots to represent the current directory and the parent directory respectively.
With the use of these dots, we can build the relative path of any file or directory from the current directory.
Following figure shows the relative path of the directories used in previous example.
Let’s take another example. Suppose a user want to access a file that is available two directories above in hierarchy from his
current directory. To access this file, he can use the following relative path.
Just like it, if the file is available in three directories above in the hierarchy, he can use the following path.
Relative path of the file or directory that is below in hierarchy always starts with a single dot followed by a forward slash as ./.
The ./ represent the current directory.
Unlike the parent directory, no symbol (dot or dots) is used for the child directory. If the file or directory is available in the directory that
is below in the hierarchy, we must have to use the actual names of child directories in path.
For example, a file named abc is available in the directory named dir1 and the directory dir1 is available in the current directory, relative path of this file will be following.
To execute a command, Shell uses current directory as the default directory.
Because of this, if the target object is available in the child directory of the current directory, we can omit the leading ./ from the relative path.
For example, to access the above file we can also use the following path.
In some situations, skipping current directory from the relative path makes it ambiguous.
In such situations, we must have to use the full relative name including the current directory. For example, to run a
script from the current directory, we must have to use the full relative name.
Let’s take few more examples to understand absolute path, relative path and the use of dots in the relative path practically.
Access Shell prompt and create a directory named dir1. Create a file named abc in this directory.
Also create a simple script named simple.sh in the current directory.
Now run the commands listed in the following table.
|cat ./dir1/abc||Print the contents of the file abc.||Use relative path. Include current directory|
|cat dir1/abc||Print the contents of the file abc.||Use relative path. Skip current directory|
|cd ./dir1||Change current directory to dir1||Use relative path.|
|cd ..||Change current directory to parent directory||Use relative path.|
|cd /home/sanjay/dir1||Change current directory to dir1||Use absolute path.|
|cp ./dir1/abc .||Copy the file abc in current directory||Use relative path.|
|./simple.sh||Run script from current directory||Use relative path|
Following figure illustrates above practice.
That’s all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social network.
Full Version EX300 Dumps
Try EX300 Dumps Demo