Skip to main content
Explain

Absolute vs Relative Path in Linux: What's the Difference?

In this essential Linux learning chapter, know about the relative and absolute paths in Linux. What's the difference between them and which one should you use.

Abhishek Prakash

Warp Terminal

Path is one of the most essential concepts in Linux and this is something every Linux user must know.

A path is how you refer to files and directories. It gives the location of a file or directory in the Linux directory structure. It is composed of a name and slash syntax.

/home/abhishek/scripts

As a user, you'll have to use the path when you want to access a certain file or directory or when you have to give the location of a file or directory to a command or script.

cat /home/abhishek/scripts/my_script.sh

Remember, if the path starts with slash "/", the first slash denotes root. The rest of the slashes in the path are just separators. Beginners often get confused between the root slash and the separator slashes.

In the above diagram, you have the first path that starts with root slash. There is another path that doesn't start with / (i.e., root).

Both are correct. The first one is absolute path and the second one is relative path. Let's have a detailed look at them.

Absolute and Relative Path in Linux

The Absolute path always starts from the root directory (/). For example, /home/abhishek/scripts/my_scripts.sh.

A relative path starts from the current directory. For example, if you are in the /home directory and you want to access the my_scripts.sh file, you can use abhishek/scripts/my_scripts.sh.

Understanding the difference between absolute and relative paths

You know that the directory structure in Linux resembles the root of a tree. Everything starts at root and branches out from there.

Now imagine that you are in the directory abhishek and you want to access the my_scripts.sh file.

The absolute path is depicted in the green dotted line and the relative path is depicted in the yellow dotted lines.

Suppose you want to see the properties of the file my_script.sh using the ls command.

You may use the absolute path that starts with the root directory (/):

ls -l /home/abhishek/scripts/my_script.sh

Or, you can use the relative path (which starts from the current directory, not /):

ls -l scripts/my_script.sh

Both commands will yield the same result (except for the path of the file).

In Linux, file names are case sensitive and can have uppercase, lowercase, numbers, periods, dashes, underscores and most symbols except slash (/). Slash (/) is reserved for root and for separating directories in the path.

Using relative path with . and .. directories

Let me show another examples to explain the difference between absolute path and relative path. But before that, you should know about two special relative paths:

  • . ( single dot) denotes the current directory in the path.
  • .. (two dots) denotes the parent directory, i.e., one level above.

Things will be clear in a moment. Take a look at the scenario. In this one, you want to go to the directory prakash from the directory abhishek.

You can use the cd command to switch directories. The absolute path is quite evident here:

cd /home/prakash

To use the relative path, you'll have to use the special relative path:

cd ../prakash

Why use ..? Because a relative path requires direction from the current directory and you have to tell the cd command to go up a level before going down. The .. brings you to the /home directory and from there you go to the prakash directory.

I hope things are a lot more clear now.

The absolute path always starts with root (/) and will remain the same irrespective of your current location. The relative path depends on your current location.

Which one should you use? Relative path or absolute path?

To be honest, there is no straightforward answer to this question. It really depends on the situation.

If you are deep down in directory hierarchy and you have to move a level up or down, using the relative path will be easier.

Suppose you are in /home/username/programming/project/interface/src/header directory and you have to access something in /home/username/programming/project/interface/bin directory. Using the relative path would save you from typing all those lengthy directory name and you can simply use ../../bin here.

But if you have to access something in the /usr/bin directory from /home/username/programming/project/interface/src/header directory, using something like ../../../../../../usr/bin will not be a wise thing to do. Using the absolute path is the sensible thing to do in this case.

Another case is using the paths in scripts or programs. When you are sure of the location, use the absolute path. If your project has several folders and you are required switching between directories, you may use the relative path here because you do not where the end user will be copying all the project files, in the home directory or in some dev directory.

In other words, you should know both methods and use the one that is going to be easier based on your scenario.

I hope you have a better understanding of path and navigation in Linux now. If you have questions or suggestions, please leave a comment.

Abhishek Prakash