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 www.maidnearme.ca/ relative path.
In scheduled tasks / automated jobs like scripts, we need to use absolute path to avoid unexpected results.
An absolute path is defined as the specifying the location of a file or directory from the root directory(/).
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
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 /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 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.