Absolute vs Relative Path in UNIX/Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

In Linux, you might already be navigating through different directories without knowing whether an absolute or relative path is being used.

It becomes important for you to understand them when you are working with scripts, navigating through different user or root directories, or creating symbolic links.

What is the “Path” in Linux?

The path is the location that is pointing towards files or directories in your Linux filesystem hierarchy, which is composed of slash (“/“) syntax and directory names.

/home/linuxtldr/scripts/myscript.sh

In the above case, the path is pointing towards the “myscript.sh” file from the root directory.

If you are wondering, where is the root directory specified in this path? Instead, it begins with home directories.

You must know that whenever you specify a slash before specifying the file path, the first slash will be denoted as the root directory, and the rest of them will be considered separators.

Absolute path

In the above picture, the path starts with (“/“), representing the root directory, and the rest of the slashes are used as separators.

If you are in your present home directory (ex: “/home/linuxtldr“), you will not place (“/“) at the beginning of the path but instead use the directory name.

Relative path

Both are correct ways to specify the directory path, except the first is the absolute path and the second is the relative path.

Absolute vs Relative Path in UNIX/Linux

In Linux, the absolute path will always start with (“/“), representing the root directory.

/home/linuxtldr/scripts/myscript.sh

While the relative path refers to the present directory. For example, if you are in your home directory (ex: “/home/linuxtldr“) and want to access the “myscript.sh” file, you will use the relative path to give the direction of the file from the present directory.

scripts/myscript.sh

Using Relative Path with . and .. Directories

In relative path, you can also navigate from the current or parent directory using the single “.” or “..” dot.

For example, if you are in your home directory “linuxtldr” from the full “/home/linuxtldr/scripts/myscript.sh” path.

Linux filesystem hierarchy

Than:

  • . (single dot) refers to the current directory in the path (ex: “linuxtldr/“).
  • .. (double dot) refers to the parent directory in the path (ex: “home/“).

If you want to navigate to the “scripts/” directory, either you can specify the directory name “scripts/” or use “./scripts/” to specify the directory name.

$ cd scripts/

#OR

$ cd ./scripts/

Do not forget to place (“.“) at the beginning of “./scripts/” if you are using this approach to navigate in the “scripts” directory.

Another example: if you want to navigate to another home directory (ex: “jake“), you can either specify the absolute path or use the double dot to refer to the parent directory.

$ cd /home/linuxtldr/jake/

#OR

$ cd ../jake/

When to Use a Relative or Absolute Path

It all depends on the situation. For example, if you are in a deep hierarchy of the Linux filesystem, like “/home/linuxtldr/Downloads/scripts/server/aws/“, and want to move one or two steps above from your current position, a relative path will be much easier.

However, if you want to move into system directories like “/var/” or “/usr/“, an absolute path will be more effective.

One more thing you must know: if you are creating symlinks for any file or directory in your system, you must specify the absolute path pointing towards your file or directory; relative paths don’t work with symlinks.

Even if you are executing any executable program or script, you can either use the absolute path pointing towards the program or script or the relative path with a single dot (ex: “./script.sh“) to execute your program or script.

If you don’t define the current path in relative position using the single dot (“.“) while executing a program or script, shell will first search for the file in the list of directories defined in your $PATH environment variable.

If any program sharing a similar name is found, that will be executed, so always specify the relative position while executing any program or script.

That’s the end of this article.

I tried to keep it short and simple; however, if you have any suggestions or tips to add to this article, feel free to tell us in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.