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.