Relative vs. Absolute Path in Linux: What Is the Difference?

Absolute and relative path names are two ways to specify the virtual address of a directory or file on Linux. But what’s the difference between these two path types, and which one should you use in your commands? Let’s find out.

Relative and Absolute Paths in Linux

If you’ve ever used any argument-based Linux command like cd or ls, you’d know there are multiple ways to specify the path to a directory or file.

You might either prefer sticking to normal path expressions or, if you’re an advanced Linux user, employ complex regular expressions to pass a file or directory name to a program. Either way, there are only two ways to specify a directory path on Linux: absolute path and relative path.

Absolute Path Expressions

An absolute path always starts from the root directory and ends at the actual directory or file you want to point to. While using absolute path expression, you need to type all the sub-directory names present inside the hierarchy that lead up to the final location.

For example, if you want to point to /myfolder/folder2 inside the /var/www directory using absolute path expression, you’d use:


…where the / at the beginning of the expression refers to the root directory on Linux.

Relative Path Expressions

Relative path, on the other hand, refers to a path expression that uses another path (usually the present working directory) as its root or base. As the name suggests, it is “relative” to an existing path on your system.

For example, if you’re currently inside the /var/www directory and need to point to /myfolder/folder2, you’d use:


…where . denotes the current directory you’re in. You can change the present working directory using the cd command.

Apart from a single period (.), relative path expressions also employ double periods (..) to refer to the parent directory.

For instance, consider you’re inside the /www folder inside the following directory structure:


In the above-stated path expression, . will denote the /www directory whereas .. will point to the /var directory. If there’s another directory /random inside /var that you need to access while you’re inside /var/www, you’d use:


Simple as that.

Absolute vs. Relative: Which Path Expression Is Better?

Although path expressions may seem simple and unimportant to you as a desktop user, there are various use cases where using the wrong path name might result in a lot of unsolicited inconveniences. Web servers are a good example.

As a web developer working on a project hosted on a Linux server, you need to access files inside the project directory. Here, using relative path expressions is the standard as any change in the sub-directory names or the domain name of the project can lead to broken links.

You can understand this with a simple example that desktop users might relate to better. The / character denotes the root directory on a Linux system. Imagine if you suddenly change that and make the / character point to the home directory. Many of your commands containing absolute path expressions will fail to work since / has now been reassigned to a different directory.

Another advantage of using relative path expressions is since they’re relative to the current working directory, it saves you time that you would otherwise spend typing out unnecessary characters.

The only catch here is the complexity and learning curve associated with relative expressions. But that’s also common in absolute path expressions; to become a master at using absolute paths, you need to be well-versed with the Linux directory hierarchy. You can use the tree command to view the entire Linux directory tree and make absolute path expressions easier for yourself.

Understanding the Linux Directory Hierarchy

The Linux directory structure is common to all machines running operating systems based on Linux. You can use a command like ls or tree to learn more about how the files and folders are structured on your computer.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *