An Overview of Relative Path in Linux [The Complete Guide]

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. It can be an absolute path, which starts from the root of the file system, or a relative path, which starts from the current working directory. In this article, I will discuss the relative path in Linux.

Types of the Path

There are two types of paths in an operating system.

  1. Relative paths.
  2. Absolute paths.

What is the Relative Path in Linux?

In Linux, a “relative path” is a file or directory path that is specified relative to the current working directory rather than an “absolute path,” which 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.Showing the file system of my machine.A. 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.

B. 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 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 a 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.Showing a visual representation of relative path.Let the directory named “Current” is 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.”.Showing the process of changing directory with relative path in linux.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 some real-life use of “relative path” with different examples.

Remove a file from a Directory Using the Relative Path

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”.Visual represent of removing a file using the relative path.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. Steps that will help you to do the same are mentioned below.

Steps to Follow:

➊ Open the Ubuntu Terminal.

 Next, run the following command.

rm -v ../doc/doc.txt

➌ Then, press the ENTER key.

Output:

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.Showing that I have removed the doc.txt file from my current location using the relative path of the doc.txt file.

Move a File to a Directory Using the Relative Path

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.Visual representation of moving a file.I want to move the “myinfo” file to the directory named “Dest.”. You can do so by following the steps given below.

Steps to Follow:

➊ At first, launch the Ubuntu Terminal.

 Next, run the following command.

mv -v myinfo ../Dest.

➌ Finally, press the ENTER key.

Output:

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.Showing that I have moved the “myinfo” file from my current location to the “Dest.” directory using the relative path.

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.Visual representation of coping a file.Now, I will make a copy of the “file” to the “Dest.” directory. The steps for copying the file using a relative path are as below.

Steps to Follow:

➊ Open the Ubuntu Terminal.

 Next, run the following command.

cp -v file ../Dest.

➌ Then, press the ENTER key.

Output:

In the image below, you can see that I have made a copy of the “file” to the directory “Dest.” using the relative path.Showing 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. By 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.


Similar Readings

Rate this post

Tags:

S. M. Amdadul Islam

S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

We will be happy to hear your thoughts

Leave a reply

LinuxSimply
Logo