Absolute and Relative path in Linux

A path is a unique location to a file or a folder in a file system of an OS. We can represent the file path using two methods. Absolute path and relative path.

Absolute path always refer the the same destination, since it is start with “/” ( root directory ). The output of the commands using absolute paths are same for all the processes, even if the current working directory is changed too.

Relative path using the current working directory saved for the process. Current working directory changes with different process, so the result must be different if we use relative path.

In scheduled tasks / automated jobs like scripts, we need to use absolute path to avoid unexpected results.

Absolute path

An absolute path is defined as the specifying the location of a file or directory from the root directory(/).

eg: /home/myhome/file.txt

Relative path

Relative path is defined as path related to the present working directory(pwd). For example, current working directory is ‘/home/myhome’ then the file.txt file can be represented as

eg: file.txt

Absolute path Vs Relative path

Example:

Assume our current working directory is “/home/myhome/” and we can access messages ( log file ) using the following methods.

# pwd
/home/myhome
# tail /var/log/messages ( Absolute path )
# tail ../../var/log/messages ( Relative path )

From the above example absolute path is easy, but a file in the same working directory can be accessed using relative path. One of the benefit of using absolute path is, we can reuse these commands from history, even if we change the current working directory. If we use the relative path, we cannot use the commands from the history.

About Suhesh KS 24 Articles
Mr. Suhesh KS is Linux System Administrator by profession with 11 Years of work experience in Linux system administration in web hosting, data center and data warehousing industry and have worked with reputed support companies. He has wide range of skills include team management, system administration ( Linux ), programming ( bash, perl, php, java ), web hosting, data center support, seo analysis.