In an OS (Operating System), a path is a string that includes the names of multiple directories and subdirectories separated by slashes (/ for Unix or \ for Windows) and specifies the location of a file or directory in the file system. There are two types of paths in Linux: absolute and relative paths. The absolute path starts from the root of the file system, but the relative path starts from the current working directory. In this article, I will discuss the relative path in Linux.
What is Relative Path in Linux?
A relative path in Linux is a file or directory path that is specified relative to the current working directory rather than an absolute path that specifies the exact location of a file or directory from the root of the file system. A relative path starts from the current working directory and uses relative directory names to specify the location of a file or directory and is useful in cases where you want to specify the location of a file or directory in relation to the current working directory.
Absolute Path vs Relative Path
Absolute and relative paths are two ways to specify the location of a file or directory in a file system. In the image attached below, you can see a visual representation of the relative and absolute paths. I am currently in the “Current” directory and want to access the directory named “Dest.”. I can do it in two different manners which are discussed below:
1. Absolute Path: An absolute path specifies the exact location of a file or directory from the root of the file system. An absolute path always starts from the root directory, represented by a forward slash (/) in Unix-based systems. In the image above, the absolute path for the “Dest.” directory starts with the root, then home, then amdadul, and lastly the “Dest.” itself.
2. Relative Path: On the other hand, a relative path specifies the location of a file or directory in relation to the current working directory. A relative path uses relative directory names and does not start from the root directory. In the above image, the “Dest.” and “Current” directories are located under the amdadul directory. So, to move from the “Current” directory to the “Dest.” directory first I have to move to the amdadul directory and then to the “Dest.” directory
Working Process of Relative Path in Linux
As I mentioned above, in Linux, relative paths are used to specify the location of a file or directory relative to the current working directory. The current working directory is the directory in which the user is currently located and is represented by a dot (.) in the relative path. Two dots (..) represent the parent directory of the current working directory and the forward slash (/) represents the file system hierarchy in the relative path.
Let the directory named “Current” be my working directory and I want to move to the directory named “Dest.”. For this reason, I will execute the command “cd ../Dest.”
. As you see in the following image, my working directory has changed to “Dest.”.
It is because when the cd command gets the double dots (..), it goes to the parent directory and then the /Dest. directory.
Useful Applications of Relative Path
In the following section, I will show you 3 practicle uses of “relative path” with different examples:
1. Using the Relative Path to Remove a File from a Directory
Let the files be organized in the following manner, as shown in the image below. Currently, I am in the directory named “current” and want to remove a file named “doc.txt” from the directory named “doc”:
I can remove the doc.txt file from my current location by using the relative path of the doc.txt file with the rm command in Linux. Next, run the following command:
rm -v ../doc/doc.txt
In the image below, you can see that I have removed the doc.txt file from my current location using the relative path of the doc.txt file:
2. Using the Relative Path to Move a File to a Directory
You can move any file from your current location to a directory by defining the relative path of the directory with respect to your location. Let’s say I am in the directory name “doc” containing two files named “myinfo” and “Penguin” which is shown in the image below:
I want to move the “myinfo” file to the directory named “Dest.”. So, run the following command:
mv -v myinfo ../Dest.
In the following image, you can see that I have moved the “myinfo” file from my current location to the “Dest.” directory using the relative path:
3. Using the Relative Path to Copy a File to a Directory
Copying any file to a directory is a simple task using the relative path of that directory. Currently, I am in the directory named “Current” of the file system shown in the following image:
Now, I will make a copy of the “file” to the “Dest.” directory. Run the following command:
cp -v file ../Dest.
In the image below, you can see that I have made a copy of the “file” to the directory “Dest.” using the relative path:
Conclusion
The relative paths in Linux provide a flexible and effective way to specify the location of a file or directory in the file system. Starting from the current working directory and using relative paths allow you to specify the location of a file or directory in relation to the current working directory, rather than in relation to the root of the file system. Understanding how to use relative paths can be valuable for developers, administrators, and other users who work with the Linux file system and need to specify the location of files and directories in a flexible and dynamic way.
People Also Ask
What are the 2 types of paths in Linux?
There are two types of paths in linux: absolute paths and relative paths. Absolute paths start from the root directory (/) and provide the full directory structure, while relative paths describe the location of a file or directory relative to the current working directory.
How to check the path in Linux?
To check the path in Linux, you can run the pwd
command in your terminal. It will print the full path of the current working directory.
Which is an absolute path?
In Linux, an absolute path is the full path from the root directory to a particular file or directory. Basically, it starts from the exact spot on your hard drive and walks you through the file system until you get to the destination.
Is absolute or relative path better?
Actually, both serve different functions. A relative URL is used within a website to move a user from one location within the same domain to another. Absolute links are used when the user wishes to navigate to a page located outside of the server.
Similar Readings
- What is Directory in Linux [A Complete Overview]
- What is Alias in Linux? [Creating, Removing & More]
- An Overview of Absolute Path in Linux [The Complete Guide]
- How to Archive in Linux Using TAR [Archiving, Extraction, Compression]
FUNDAMENTALS A Complete Guide for Beginners