Home » Bash Relative vs. Absolute path

Bash Relative vs. Absolute path

by Online Tutorials Library

Relative vs. Absolute path

Before creating a first Bash Script, you should be well aware of the shell navigation and the difference between the relative and absolute path for an intended file. So let’s understand what they are?

What is the Path?

A path to a file is a merged form of slash (/) and alpha-numeric characters. It determines the unique location of a file or directory in an OS filesystem.

Absolute Path

An Absolute Path is a full path specifying the location of a file or directory from the root directory or start of the actual filesystem.

Example: /home/tutoraspire/Desktop/CollegeStudent

  • An Absolute path of any directory always starts with a slash (/) representing the directory root. Besides this, all slashes in the directory path separate the directories.
  • All directories names in the absolute path are written in the hierarchy order. The parent directory name is written on the left side.
  • The last name in an absolute path may belong to a file or directory. Besides the last name, all names belong to the directories.
  • The absolute path of the current directory can be determined by using the pwd command.

Relative Path

The relative path of a file is its location relative to the current working directory. It never starts with a slash (/). It begins with the ongoing work directory.

Example: Desktop/CollegeStudent

  • Single Dot (.) resolves to the current directory.
  • Double Dot (..) resolves to the parent directory of the present work directory.
  • Tilde (~) represents the home directory of logged in user.

Relative Path Vs Absolute Path:

The topmost directory in any filesystem is the root directory denoted by the slash (/). You can describe the location of any file or directory in the filesystem with the absolute path. That means you will take every step starting from the root directory or the absolute beginning of the filesystem.

An absolute path is unambiguous and may be inconvenient to work with, especially if you are working with deeply nested directories. To get a simpler mode of the things, you can use the Relative path instead. Such that, if you are working with files in or near the present working directory, this can save you from a lot of typing.

Every time you’ve referred to a file by just its name, you’ve been using a relative path. This is the most straightforward sort of relative path. The shell looks for the specified file name within the current directory.

See these examples so that you know how the relative path is convenient over the absolute path.

1. Absolute Path

Relative vs. Absolute path

2. Relative Path

Relative vs. Absolute path

Shell Navigation:

There are two commands (cd and pwd) which help in the navigation of GNU/Linux filesystem thoroughly, where,

  1. cd is used for changing the directory,
  2. pwd for printing the current working directory.

We can easily navigate throughout the filesystem with the help of these commands using both the relative and absolute path. Just follow the given basic example.

Here, you can see how cd and pwd command help in navigation for different directories, i.e., root, parent, and home.

Relative vs. Absolute path

Next TopicBash Comments

You may also like