Linux Commands

What is a Relative Path in Linux

What is a relative path in Linux

Path describes the exact location of a directory or a file in the Operating System. When working with the command line in Linux, files and directories are accessed by using paths for quick navigation. Linux supports two paths: relative path and absolute path.

The path to a file or a directory relative to the present working directory is called a relative path, while, the complete path to a file or a directory from the root directory is called an absolute path.

This article will discuss the relative path in the Linux environment.

What is a Relative Path in Linux?

Relative path defines the route to access a particular file or directory from the present working directory. Relative paths are shorter as compared to absolute paths, for example, if we want to access a file “helloworld.cpp” in the Downloads directory, we can access this directory by using absolute and relative paths as follows:

How to Access File/Directory Using Absolute Path?

If we are in the home directory and want to access a file “helloworld.cpp” in the Downloads directory, we can access this file by absolute path as below:

$ ls /home/debianuser/Downloads

How to Access File/Directory Using Relative Path?

If we are in the home directory and want to access a file “helloworld.cpp” in the Downloads directory, we can access this file by the relative path as below:

$ ls Downloads

Relative paths are not fixed and they change depending on the working directory. We can check our present working directory by the following command:

$ pwd

How to Adjust the Relative Path in Linux?

Additionally, the following symbols can be used to adjust the path:

  • . (dot) refers to the current directory, e.g., ./helloworld.cpp refers to a file helloworld.cpp in the current directory.
  • .. (dot dot) refers to the parent directory, e.g., ../Introduction/helloworld.cpp refers to a file helloworld.cpp in the Introduction subdirectory of the parent directory of the current working directory.
  • Additionally, ../../Introduction/helloworld.cpp refers to the file helloworld.cpp in the Introduction subdirectory which is two levels up from the current directory.
  • ~ (tilde) refers to the home directory, e.g., ~/Downloads/helloworld.cpp where ~ refers to home/debianuser.

What are the Advantages of Relative Path in Linux?

A few advantages of using relative paths are discussed as follows:

  • Relative paths are convenient when working within a specific directory. It is easier to navigate in the file system without breaking the path as relative paths are relative to the present directory.
  • Relative paths are shorter as compared to relative paths as they don’t include the entire path from the root directory. Working collaboratively can be very useful as it saves time, reduces typing errors, and avoids confusion.
  • Relative path allows scripts to run from different locations without the need to modify the path of the script as files and directories are referenced relative to the location of the script.
  • Relative paths allow you to move a website from one server to another server with ease during the web development process as with the same directory structure, the file path remains the same.

Conclusion

Relative and absolute paths are used while navigating through files in the Linux environment. Relative paths are more convenient and are preferred when working within a specific directory. In this article, the relative path along with the usage and advantages are also discussed.

 

Similar Posts