Absolute and Relative path in Linux

June 3, 2016 by No Comments

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 www.maidnearme.ca/ 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


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

# pwd
# 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 azlimo.com. 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.