Updated April 14, 2023
Introduction to Linux Relative Path
In the Linux operating system, the relative path is used to represent the current working directory. There is no concept to start or share the Linux relative path from the “/” (starting from root location). Instead, the relative path location or value starts with the current or present working directory. In the Linux ecosystem, we can use the different shortcuts for the relative path.
1. While writing any shell jobs or application-level jobs. Please do not use the relative path of file or directory location.
2. While exporting the environment variables, please do not keep the relative path of the variable. Instead, always keep the absolute file or directory path.
Syntax of Sort Command
cd [ directory location ]
cd .or cd ..
- cd: We are using the “cd” (change directory) keyword in the Linux relative path. It will help to change the directory location from the current working directory to the new or destination directory. There is no need to add the complete path or location from the “root” level.
- cd . “single dot”: We can use the single dot with the “cd” command in the Linux environment. It will represent the current working directory.
- cd .. “double dot”: We can use the double dot with the “cd” command in the Linux environment. It will represent the parent directory.
How Linux Relative Path Works?
In the Linux environment, we are having different ways to travel from one directory location to another directory location. For doing this, we can use the Linux absolute path or the Linux relative path. The Linux relative path is useful for the shortcut transmission of files or directories. There is no need to add the complete location of the file. Generally, the Linux relative path is not starting with the “/” or root location. Instead, the relative path starts with the current working directory. For important shell jobs or the application level jobs, it is recommended to use the absolute path only. Please do not use the relative path while working on such automation or shell task.
To understand it better, kindly go through with the below diagram.
As per the above file system flow, we are having the “/” location on top. From the “/” location, we have multiple directories like home, var, dev, etc., usr and having different subdirectories like test-user, log, lib, and more. Let consider, we are in the location “/home/test-user,” and we need to go to the “Photos” directory. Here, we can directly go to the “Photos” directory, i.e. “cd Photos”. Similarly, if the current working directory is “/home/test-user”, and we need to go to “August”. We can directly go like “cd Photos/August”. In the relative path, there is no need to add or start the file or directory location from “/”. We can use the different shortcut methods like single dot “.” for the current directory or the double dot “..” for the parent directory.
Examples to Implement Linux Relative Path Command
Different examples are mentioned below:
Example #1 – Relative Path Command
In the Linux environment, the relative path command is nothing but a concept. Basically, all the stakeholders are using the relative path concept with the help of the change directory command only, i.e. “cd” commands. Therefore, it is the most common way to use the relative commands or concepts in the Linux environment.
Command :
pwd
cd file
Explanation :
We are using the two commands to explain the Linux relative path, i.e. “pwd” ”cd file”. As per the below screenshot 1 (a), we are in the “/root/data” directory. There are different ways to access the file or directory. But we are using the relative path to access it. We need to access or move from the current working directory “/root/data” to “/root/data/file”. Here we are not using the “/” location with the help of a relative path; we can directly go to the “file” directory without using the “/” location. Kindly refer to screenshot 1 (b).
Output :
Screenshot 1 (a)
Screenshot 1 (b)
Example #2 – Relative Path Command–With Single Dot “.”
In the relative path, we have the functionality to represent the current working directory. We can use a single dot to represent it. For the same representation, we need to use the single dot “.” with the “cd” command. It is one of the methods to represent the Linux relative path.
Command:
cd .
Explanation:
As per the above command, we are using the single dot “.” with the cd command. In the relative path, the single dot “.” represents for a current working directory, i.e. “/root/data”. Therefore, when you enter the above command, it will only represent the current working directory, i.e. “/root/data”.
Output:
Example #3 – Relative Path Command – With Double Dot “..”
In the relative path, we have the functionality to represent the parent directory. With the help of the double dot “..”, we can represent or move to the parent directory. To do such a working directory or location change, we need to use the “cd” command with the Linux relative path concept, i.e. “..” double dot.
Command :
cd ..
or
cd ../../
Explanation :
As per the above two commands, in one command, we are using the double dot (“..”) with the cd command, and in another command, we are using the combination of the double dot. As per the first command, we are able to represent the parent directory. Currently, the working directory is “/root/data/file”. Now we can represent the parent directory path with the help of the first command, i.e. “cd ..” (Refer screenshot 2 (a)). We can also use the combination of the double dot as well (refer to screenshot 2 (b)).
Output :
Screenshot 3 (a)
Screenshot 3 (b)
Conclusion
We have seen the uncut concept of “Linux Relative Path” with the proper example, explanation, and command with different outputs. The Linux relative path the path is defined with the current working directory (for a present relative path, we can use the “pwd” command). There is no need to start the relative path with “/”.
Recommended Articles
We hope that this EDUCBA information on “Linux Relative Path” was beneficial to you. You can view EDUCBA’s recommended articles for more information.