What are the Absolute and Relative paths, and why do we use them?

While following some of the articles, you may frequently hear about absolute and relative paths. They may suggest you to use an absolute path or a relative path, but what is exactly this term, and why should you care about it? 

And what is the key difference between absolute and relative paths, and how can you exactly use the following fundamental concept to get a more proficient way of traversing (moving) through a directory? 

So let’s clear up this topic with a basic command that everyone has used till now.

What is an Absolute Path?

When you focus on the word “absolute”, which means complete as per the English dictionary, and if you combine “absolute” with “path”, it turns into a complete path. If someone has to reach a particular destination or location, then he or she needs to be aware of the complete path. Otherwise, they may end up in the wrong location.

Which is not correct at all. 

The same goes for a file. If you are holding a certain file at XYZ location, then you should provide a proper path to the command to access it, else you may find an error.

Most of the Linux distributions follow the File Hierarchy System (FHS), so you may not find the directory with something “C:” or “D:” drive like in Windows. 

Here you will find the “/” forward slash, which means root directory, and that’s the reason all the files and directories are connected to the root directory. Even if you mount a USB or external drive, it will end up in the /mnt/ directory.

That’s a reason we never suggest you to run the command sudo rm -rf / which will completely wipe out your root directory. If you want to get into more detail, then check out this article: What will happen if I run “sudo rm -rf /” on Linux?

Let me get you to the terminal and decode the absolute path of your current directory. Once you open the terminal and run the pwd command, you will find the output like the below snippet.

/home/trendoceans

If you furcate the above path, you will find “/”, which is the root directory, and “home,” which is the subdirectory of the root.

Under the home directory, you will find the child directory, which is separated by forwarding slash (/) “trendoceans” or your username.

In short, wherever you find a directory or file that starts with “/” in the root directory, accept it as an absolute path. 

And if I ask you, is /etc/apt/sources.list following an absolute path? If yes, then please do explain your answer in the comment section. 

Let’s take an example of an absolute path to create a directory, move into the newly created directory, and the last task is to create a new file using the touch command by following the principle of an absolute path.

I’m creating a new directory in my home directory, which is “/home/trendoceans”, so to create it, let me invoke the below code, and after that, I’ll change my current directory to “/home/trendoceans/createdirectory”.

$ mkdir /home/trendoceans/createdirectory 
$ cd /home/trendoceans/createdirectory

And last, let me create a file inside /home/trendoceans/createdirectory using the touch command.

$ touch /home/trendoceans/createdirectory/newfile

In the end, our result will look like the below image:

Example of Absolute way
Example of Absolute way

Really, do you want to follow the above method to create, access, and delete files? There is no way anyone can agree with these cumbersome steps. 

Here, a relative path comes into the picture to help you out and save you from typing much.

What is a Relative path?

A relative path is a mirror of an absolute path where you do not need to mention a complete path starting from the (/) root directory.

You can createaccess, and delete any file directly from the current path, but how? In contrast to the absolute path, you do not need to use the “/” root directory. You can simply declare the filename or directory as per the command arguments.

On top, we have discussed how you can create a directory using an absolute path, but now we will see a much simpler way to perform the same task without typing much.

So, first, open your terminal application, and you will find the interface very similar to mine, although it may differ from distribution to distribution. 

From the below image, you can see the tilde (~) next to hostname, which means we are currently in the home directory, and it gets tracked whenever we change our current directory to something else.

Show home directory
Show home directory

Therefore, first create a new directory using the mkdir command without providing a complete path, and after that, change the current path to “duplicatedirectory” using the cd command.

$ mkdir duplicatedirectory
$ cd duplicatedirectory

As you can see, a new directory has been created in the home directory without specifying the absolute path. If you have noticed, tilde (~) has changed to (~/duplicatedirectory).

This means our current directory is “~/duplicatedirectory” in a relative path, but in the absolute path, it will look like “/home/trendoceans/duplicatedirectory”.

Create directory using relative path
Create a directory using the relative path

It’s quite easy and simpler than the absolute path.

If you want to change your conventional way of accessing a directory, then two special characters can be very helpful to use as a relative path. 

  1. The single dot (.) represents the current directory.
  2. The double dot (..) represents the parent directory.

To explain it to you briefly, I will create three directories in the current path, and you can also create directories if you want to practice along with me.

$ mkdir dir_1/dir_2/dir_3

Now let me change my directory to dir_3

$ cd dir_1/dir_2/dir_3/

After getting to dir_3, I realize I don’t have any files here, so let me go to the Downloads directory, which is located ~/home/trendoceans/Downloads.

The above location can be reached in three different ways; the first two ways will be relative, and the last will be absolute.

$ cd ../../../Downloads/
$ cd ~/Downloads/
$ cd /home/trendoceans/Downloads/

And from the Downloads directory, I have moved the file to dir2 using special characters. 

trendoceans@shen:~/Downloads$ mv file.txt ../dir_1/dir_2/

Similarly, you can read a file from a directory using a special character like the below code snippet:

trendoceans@shen:~/dir_1/dir_2$ cat ../../../../etc/apt/sources.list

Instead of that, you can use the absolute path, which looks much cleaner and is much simpler to understand than the relative path.

$ cat /etc/apt/sources.list

Wrap up

That’s all for this Absolute and Relative path guide. I hope I was able to clear up your concept. Both of them have their own advantages, and it’s up to you how you want to use them. 

If something is left to add, then please do let us know in the comment section.

Leave a Reply