An absolute address refers to a document or folder’s address independent of the current working directory; in actuality, it is related to a root folder. Its name comes from the fact that it includes the full address of a document or folder. Also, it’s known as a complete pathname and an absolute pathname since it always begins at the very same location, which would be the root folder. Absolute routes include most of the details needed to locate the assets referenced via an absolute URL. When referring to websites hosted on a domain else than your own, the absolute path should be cast off. To write the absolute path, you have to use a slash “/” to start it, representing the root directory.
A relative path specifies where a document or directory is located concerning the current working directory. It is indeed best cast-off to link to sites on the very same domain, particularly on certain portions of websites where the documents’ connections to one another never alter. Despite absolute routes, relative routes only carry content useful to the present content on the very same site, obviating any need for a complete absolute path. In basic terms, a relative path is a route that is relative to the current web page’s position.
Let’s have some clear examples to elaborate the concept of Absolute and relative path. Hence, log in from the Ubuntu system first and then open the terminal. The terminal application can be opened using “Ctrl+Alt+T” or indirectly from the applications. When you open the terminal, you must be located at the root directory of your Ubuntu 20.04 system. Hence, to check your current location while working, we will be using the “pwd” command in the shell as below. You can see, we are currently at the location of the root directory, which is specified by the user name “aqsayasin” followed by the “home” directory.
Let suppose you have a file “test.txt” currently residing in your home or root directory where you are currently residing. This means, when you want to check the contents of the file “test.txt” from the current location, it should show its contents. Hence, we have to check if it will be opened using a cat query in the shell or not. So, we tried the command below and got the contents of a file successfully.
Suppose you move the file to the Documents folder and check if the cat query will show its contents. You will get the below error saying, “No such file or directory”. This is because we haven’t used the absolute path.
But you can also show the file contents using slash within the path to the folder where the file resides, e.g., Documents. Hence, the below form of the path will work perfectly with a cat. You have to understand that the file’s location is resolute about root because of the first slash sign “/”. Every slash sign indicates that we are dropping one level throughout the file system to every such /, as below, where “aqsayasin” is one level beneath “home” and hence two levels beneath “root”.
The relative pathname is a Linux shorthand that takes the current or parental folder as a base and provides the route. A few of these enigmatic characters are used in a relative pathname:
- Single Dot: The current folder is represented by a single dot.
- Double Dot: The parental folder is represented by two dots.
That implies that we’re presently in the folder /home/aqsayasin; we can even use the “..” option within the “cd” query to go to the parental location /home. So let’s do this by checking the current directory using “pwd”, and you can have a look that we are currently at /home/aqsayasin.
Let’s use the double dots “..” in the “cd” command to move towards the parent directory as:
This will move to the /home directory as below. You can also confirm it using the “pwd” command.
Let’s have the same concept in another example. First, move to the Documents folder using the “cd” query in your terminal shell below.
Now check your current location with “pwd”, and you can see we are at Documents folder two-level below from the home directory and three-level below from the root (as three slash signs are used). Now you can open any file that has been residing in this folder.
Let suppose you want to go two levels up in the path. For this purpose, we have to use double dots two times in the “cd” query with the slash sign between them. The first double dots represents the parent of the “Documents” folder, which is “aqsayasin”. And, double dots after the slash sign represents the parent of folder “aqsayasin”, which is “home”. Hence, we should be moved to a home directory using this query.
The blue highlighted part of the image shows the “home” directory, which is our current location. On the other hand, you can check it via “pwd” as below.
Let’s have another example. Let suppose you are at the root directory of your system right now. You can confirm it via “pwd” instruction as well.
Let’s move to another folder. Let’s move to the “Pictures” folder using the same “cd” instruction in the shell. Now you are in the Pictures folder. You can also check it using “pwd” again. The blue highlighted part also shows that you are in the Pictures folder.
From the above image, you can see that the parent of a “Pictures” folder is “aqsayasin”. Here is the twist in this example. Let suppose you want to move towards the Document folder directly from the Pictures folder. For this reason, we should use double dots in our cd command along with a slash sign to get to the parent of a folder “Pictures”, which is “aqsayasin”. On the other hand, we have mentioned a folder name, e.g., “Documents”, after the slash sign, as we want to go towards it from the Pictures folder. Remember that you cannot directly move towards the Document folder until you get to its parent folder, “aqsayasin”. Now you are at the Documents folder, as shown from the “pwd” command output.
We have done with both the paths, e.g., absolute and relative path, in this guide. We have covered enough examples to elaborate and understand the concepts of referencing them with each other.