A path in an OS (Operating System) is a string that identifies the location of a file or directory in the file system and contains the names of multiple directories and subdirectories, separated by slashes (/ for Unix or \ for Windows). In this article, I will try to give an overview of absolute path in Linux, which defines the exact location of any file or directory in any file system with respect to the root directory.
Types of the Path
There are two types of paths in an operating system.
- Relative path.
- Absolute path.
What is the Absolute Path in Linux?
An absolute path in Linux is a complete path for a file or directory that starts from the root directory (represented by a forward slash “/”) and specifies the exact location of the file or directory in the file system. An absolute path is independent of the current working directory. That means, it will always point to the same file or directory, regardless of the location of the user or process that is accessing it.
Absolute Path vs Relative Path
Absolute and relative paths are two ways to specify the location of a file or directory in a file system. In the image attached below, you can see a visual representation of the relative and absolute paths. For example, I am currently in the “Current” directory and want to access the directory named “Dest.”. I can do it in two different ways, which are discussed below.A. Absolute Path
An absolute path specifies the exact location of a file or directory from the root of the file system. Moreover, an absolute path always starts from the root directory, represented by a forward slash (/) in Unix-based systems. In the image above, the absolute path for the “Dest.” directory starts with the root, then home, then amdadul, and lastly the “Dest.” itself.
B. Relative Path
On the other hand, a relative path specifies the location of a file or directory in relation to the current working directory. A relative path uses relative directories and does not start from the root directory. In the above image, the “Dest.” and “Current” directories are located under the amdadul directory. So, to move from the “Current” directory to the “Dest.” directory, first I have to move to the amdadul directory and then to the “Dest.” directory
Working Process of Absolute Path in Linux
As I mentioned above, an absolute path is a complete path to a file or directory starting from the root directory represented by a forward slash “/.” Unlike relative paths, absolute paths do not depend on the current working directory and provide a fixed location to a file or directory regardless of the user’s current location. Let the directory named “Current” be my working directory and I want to move to the directory named “Dest.”. For this reason, I will execute the command “cd /home/amdadul/Dest.”. As you see in the following image, my working directory has changed to “Dest.”.
It is because when the cd command gets the forward slashes (/), it goes to the root directory and then to the home directory, and after that to the “amdadul” and lastly the “Dest.” directory.
Practical Applications of Absolute Path in Linux
In the following section, I will show you some real-life uses of the “absolute path” with different examples.
Remove a file from a Directory Using the Absolute Path
Let the files be organized in the following manner, as shown in the image below. Currently, I am in the directory named “amdadul” and want to remove a file named “doc.txt” from the directory named “doc”.I can remove the doc.txt file from any location by using the absolute path of the doc.txt file with the rm command in Linux. Now, follow the steps mentioned below to do the same.
Steps to Follow >
➊ First, Open the Ubuntu Terminal.
➋ Next, run the following command.
rm -v /home/amdadul/doc/doc.txt
➌ Then, press the ENTER key.
Output >
In the image below, you can see that I have removed the doc.txt file from the directory named “doc” using the absolute path of the doc.txt file.
Move a File to a Directory
You can move any file from any location to a directory by defining the absolute path of the file and directory with respect to the root directory. Assume that, I am currently in the directory “amdadul” and want to move a file from the directory “doc” which contains two files named “myinfo” and “Penguin” as shown in the image below. Now, I will move the “myinfo” file from the “doc” directory to the directory named “Dest.” using the absolute path of the file and the “Dest” directory. You can do so by following the steps given below.
Steps to Follow >
➊ At first, launch the Ubuntu Terminal.
➋ Next, run the following command.
mv -v /home/amdadul/doc/myinfo /home/amdadul/Dest.
➌ Finally, press the ENTER key.
Output >
In the following image, you can see that I have moved the “myinfo” file from the “doc” directory to the “Dest.” directory using the absolute path.
Copy a File to a Directory Using the Relative Path
You can use the absolute path to copy any file to any directory without concern about the current location. Assume, I am currently in the directory named “amdadul” of the file system shown in the following image. Now, I will copy the “file” to the “Dest.” directory. The steps for copying the file using an absolute path are given below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Next, run the following command.
cp -v /home/amdadul/Current/file /home/amdadul/Dest.
➌ Then, press the ENTER key.
Output >
In the image below, you can see that I have made a copy of the “file” to the directory “Dest.” using the absolute path.
Conclusion
The absolute paths in Linux provide a complete and specific description of the location of a file or directory in the file system. Unlike relative paths, absolute paths allow you to specify the location of a file or directory in a fixed and unchanging manner. Understanding how to use absolute paths can be valuable for administrators, developers, and users who work with the Linux file system and need to specify the location of files and directories in a definite way.
Similar Readings