Best of Linux

Absolute vs Relative Path in Linux: What’s the difference?

Absolute vs Relative Path in Linux What's the Difference

Linux is an open-source operating system that provides a wide range of features, such as portability, multitasking, and security. People choose Linux over other operating systems because of its renowned features. It provides advanced network security options with the help of firewalls.

When we are working on a Linux OS, navigating through files and folders is a thing we do very often.

To get into a file or folder, we must provide a path.

This write-up will list the common distinctions between the absolute and relative paths along with practical implementation.

What is a Path?

A path describes a system’s layout like a map. In an operating system’s file system, a path is a specific address for a file or a folder. A file’s path is made up of both alpha and numeric characters.

What is an Absolute Path in Linux?

An absolute path defines the position of a file or folder from the root folder (/). An absolute path starts at the absolute beginning of our hard disk and demonstrates each and every step we take through the file system to our target location.

Let’s see some examples to understand the absolute path in a better way:

$ pwd

You can see the current path, in this case, is “/home/debian”, where the first slash “/” describes the root directory:

To access the list of available directories, execute the “ls” command:

$ ls

The output shows that the home directory of Debian holds all other child directories:

If we want to see the content of any other folder, we can do it easily. In my case, I want to check what’s inside my Picture folder.

$ ls /home/debian/Pictures

What is a Relative Path in Linux?

A relative path is used to represent the present working directory. It is beneficial for the shortcut transfer of files because it originates from the current working directory. Similar to the earlier example, we can access the contents of the Pictures folder. The command is given below:

$ ls Pictures/

It can be seen that we can achieve this in a shorter way through a relative path as shown below:

Relative paths can also be accessed through single dots(.) and double dots (..).

A single dot specifies the current directory while a double dot specifies the parent directory. The example is illustrated below:

$ ls ../

$ ls ..//..//

$ ls ./Pictures

Differences Between Absolute Path and Relative Path in Linux?

Some key differences between the Absolute path and the Relative path are discussed below:

  1. The major difference between absolute and relative paths is present in their reasoning. Relative paths are interpreted based on the current directory, and they can lead to different paths depending on that directory. While the absolute paths always refer to the same path irrespective of their current directory.
  2. Since relative paths do not begin from the root directory, they are generally shorter than absolute paths.
  3. Absolute paths are more compatible when working with scripts while relative paths are more compatible when working with long and complex files.
  4. Relative paths are more convenient for attributing files like scripts, graphics, etc., however, absolute paths are more useful for system administration tasks since they enable administrators to get to files easily.
  5. They are helpful for accessing files remotely in order to ensure the file has been accessed in the correct manner whereas relative paths are more efficient while working and accessing files within a system.

Conclusion

In an operating system’s file system, a path is a certain address for a file or a directory. The absolute path defines the position of a file or folder from the root folder (/), whereas the relative path describes the position of a file or folder with respect to the present working directory. This guide has demonstrated the difference between absolute and relative paths in Linux successfully.

Similar Posts