When handling files and directories on Linux, you must know how to work with paths for quick navigation and access to files. A path defines the route to access a given folder or file. Linux’s directory structure resembles the roots of a tree, where everything starts from the root down to the branches until you reach the target file or folder.
Today’s guide focuses on understanding paths on Linux. We will dig in on absolute and relative paths in Linux to understand their differences and how to use each when locating the path to a file or folder.
Understanding Absolute and Relative Path in Linux
The Linux file system is all about files and directories— how you access these files or folders is using paths. Every path starts from the root directory, represented as a slash (/). From the root directory, you can have the separators. You can use the relative or absolute path.
1. Absolute Path
You use the absolute path when you specify the path to a file or folder, starting with the root directory. This path begins with the root (/), followed by the separators to the other directories in the middle before you reach the target file.
To understand the absolute path in Linux, let us have an example of using the pwd command.
You will note that our current path, in this case, is /home/linuxhint. The first slash (/) represents the root directory. All users created in a Linux system get stored in the home directory. In this case, our user is named linuxhint. So, this absolute path is the home directory of linuxhint and it contains all the other child directories.
Suppose we wanted to list the contents of the Downloads directory for the named user, you can do so using its absolute path, as shown.
The absolute path in Linux allows access to files and folders in other locations without navigating into their parent directory.
2. Relative Path
Unlike the absolute path that starts from the root directory, the relative paths start from the present working directory. Thus, the relative path changes depending on your current directory.
For instance, referencing the earlier example of accessing the contents of the /Downloads, our command would change as follows, if we wanted to use the absolute path:
The target folder is in the current directory, meaning we can directly reference it without needing to add the root directory.
The relative path allows using single dot (.) and double dot (..) to specify paths up or down the current directory. These dots are not visible unless you use them to access the target file.
The single dot specifies the current directory, while the double dot represents its parent directory.
Let us give an example.
We are in the /home/linuxhint/Downloads/names path.
Suppose we wanted to list the contents of the Downloads/ without typing its absolute path, we could use the double dots for the relative path to access the parent directory with the command below.
Using the relative path in such a case is more convenient, unlike the absolute path, which would require typing the whole path as shown.
Similarly, if we wanted to move higher in the parent directory, we could add more double dots with a separator. In the below example, we have accessed the home directory.
If we wanted to access a directory in the child directory of our current directory, we could specify its relative path using single quotes.
Absolute Path vs Relative Path
The absolute path specifies the path to a folder or file starting from the root directory followed by the user’s home directory. However, the relative path specifies the path to a file or folder starting with the current directory.
Use the relative path when accessing a child or parent directory starting from the current directory. Use the absolute path to access a file or folder down the file system or from another location without switching the current directory.
This guide has discussed what the absolute and relative paths mean in Linux. We have seen the examples in each case and how to choose either option depending on the location of your target file or folder. With this post, you will quickly understand how to work with paths in Linux.