Absolute and Relative Paths in Linux | How to Reference Them?

An absolute path is a complete path that begins from the root directory. In contrast, the relative path is a path that refers to the existing directory. Absolute paths are utilized when the exact location of a file or directory is needed or if the directory/file resides nearby locations of the root folder. The relative paths are often utilized to refer to a file or directory in the working directory or a nearby subdirectory.

This article presents absolute and relative paths and how to reference them in Linux. The supported content of this article is mentioned below:

What is Absolute Path in Linux?

The absolute path refers to the root directory of the system. The absolute path comprises all the directories/subdirectories (from the root directory) needed to reach the desired file or directory.

How to Reference an Absolute Path?

To reference an absolute path in Linux, you must provide the full path starting from the root directory, represented by a forward slash ‘/’.

Example 1: Change the Current Directory Using Absolute Path 

For instance, we need to change the directory to “Downloads”, and our terminal is currently in the home directory. For this, the “cd” command is used with the complete path from the root directory:

$ cd /home/itslinuxfoss/Downloads

The output shows that the current working directory has been changed to the “Downloads” directory.

Example 2: Move a File Using its Absolute Path 

To move a file using the absolute path, specify the file name with the complete directory path. For instance, specify the “file.txt” file that is located in the “/home/itslinuxfoss/” directory and move to the “/office” directory:

$ sudo mv /home/itslinuxfoss/file.txt /office

This command uses two absolute paths, one for the source file and another for the destination.

What is Relative Path in Linux?

This path contains the directories and subdirectories needed to reach the desired directory or file from the current working directory.

How to Reference Relative Paths?

To reference a relative path in Linux, you do not need to provide the full path starting required to reach the destination from the current directory. 

Example 1: Change the Current Directory

Let’s navigate to the “Downloads” directory from the current directory (which is the home directory) using the relative path: 

$ cd Downloads

This is a relative path, as it refers to the parent directory of the existing/current directory.

Example 2: Move a File With the Help of the Relative Path 

To move a file using the relative path in the Linux terminal, execute the “mv” command by specifying the source and destination of the file:

$ mv file.txt Folder/

It moves the “file.txt” file from the current directory to the “Folder” subdirectory.

Conclusion

In Linux, the absolute path provides the exact location of a file or directory, while the relative path refers to directories and files in the current directory. To reference an absolute path, you need to provide the full path starting from the root directory, while the relative path refers to the leading path from the current directory. This article has explained the absolute and relative paths and the reference in Linux.