One of this blog follower asked us that what’s the difference between absolute and relative path?

To understand this we have to know what is a path in Linux.

What is a path?

A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.

What is an absolute path?

An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.

Some examples of absolute path:

/var/ftp/pub
/etc/samba.smb.conf
/boot/grub/grub.conf

If you see all these paths started from / directory which is a root directory for every Linux/Unix machines.

What is the relative path?

Relative path is defined as path related to the present working directory(pwd). Suppose I am located in /var/log and I want to change directory to /var/log/kernel. I can use relative path concept to change directory to kernel

changing directory to /var/log/kernel by using relative path concept.

pwd
/var/log
cd kernel

Note: If you observe there is no / before kernel which indicates it’s a relative directory to present working directory.

Changing directory to /var/log/kernel using absolute path concept.

cd /var/log/kernel

Note: We can use an absolute path from any location where as if you want to use relative path we should be present in a directory where we are going to specify relative to that present working directory.

Examples of relative path and absolute path for the same operation.

Example 1: Present location is /abc/xyz, I am want to remove /abc/xyz/read/hello.txt file.

Using relative path:

rm read/hello.txt

Using absolute path:

rm /abc/xyz/read/hello.txt

Example 2: My present location is /etc/samba and now I want to change directory to /etc.

Using relative path:

cd ..

Using absolute path:

cd /etc

Example 3: My present location is /var/ftp/ and I want to change the location to /var/log

Using relative path:

cd ../log

Using absolute path:

cd /var/log

Example 4: My present location is /etc/lvm and I want to change my location to /opt/oradba

Using relative path:

cd ../../opt/oradba

Using absolute path:

cd /opt/oradba

Example 5: My present location is /usr/local and I want to remove a abc.txt file located in this directory how can I do that?

Using relative path:

rm abc.txt

Using absolute path:

rm /usr/local/abc.txt

I hope this helps to understand the difference between Absolute and relative path.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.