An Overview of Absolute Path in Linux [The Complete Guide]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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.

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 in Linux

Absolute and relative paths are 2 ways to specify thelocation 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:Showing the file system of my Linux machine.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

How Does the Absolute Path in Linux Work?

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.Directory sequence of absolute path in linux. 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.”.Process to change the working directory using absolute path in linux.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.

3 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.

1. 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”.Visual representation of absolute path for removing a file.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:

  1. First, Open the Ubuntu Terminal.
  2.  Next, type the following command and press ENTER:
    rm -v /home/amdadul/doc/doc.txt

    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.Showing that I have removed the doc.txt file from the directory named “doc” 

2. 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:Visual representation of absolute path for moving a file. 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 running the command below in the terminal:

mv -v /home/amdadul/doc/myinfo /home/amdadul/Dest.

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.Showing that I have moved the “myinfo” file from the “doc” directory to the “Dest.” directory using the absolute path.

3. 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:Visual representation of absolute path for copping a file. Now, I will copy the “file” to the “Dest.” directory. The steps for copying the file using an absolute path are given below:

  1. At first, open the Ubuntu Terminal.
  2.  Next, run the following command:
    cp -v /home/amdadul/Current/file /home/amdadul/Dest.

    In the image below, you can see that I have made a copy of the “file” to the directory “Dest.” using the absolute path.Showing that I have made a copy of the “file” to the directory “Dest.” .

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.

People Also Ask

How to identify an absolute path in Linux?

To identify an absolute path is very simple: An absolute path begins with a forward slash (/) denoting the root directory followed by the subsequent directory names leading to the specified file or folder. For example, /home/user/documents/hello.sh is an absolute path where hello.sh is located within the documents folder, inside the user directory under /home.

How many paths Linux Support?

Linux support two types of path: absolute path and relative path.

What does a path do in Linux?

A path in Linux serves as a guide to users by providing the precise route to access files and directories. To access any files or folders, you need to specify its path.

What is the difference between absolute path and relative path in Linux?

An absolute path is the complete path of a file or directory from the root directory which is the top-level directory of the file system in Linux. Presented by the forward slash (/), the absolute path always starts with the root directory providing a full path. For example. The absolute path to the home directory of the user prem is /home/prem.

On the other hand, a relative path is a path to a file or folder that is relative to the current working directory. Relative paths are usually shorter than absolute paths and do not start with the root directory. For instance, to access file.txt in a subdirectory called logs when I am in the home directory, the relative path would be logs/file.txt.


Similar Readings

Rate this post
S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

Leave a Comment