How to Get Absolute Path in Bash

Abid Ullah Feb 02, 2024
  1. How to Get the Absolute Path in Bash
  2. The Root Directory of a File in Linux
  3. Use the readlink Command to Get Absolute Path in Linux
  4. Use the realpath Command to Get Absolute Path in Linux
  5. Use the find Command to Get Absolute Path in Linux
How to Get Absolute Path in Bash

In this Bash article, we will learn different methods to get the absolute path in Linux. We will also learn some different Linux commands to get a file’s absolute path.

Before we start, we need to understand the basic concepts of absolute and relative paths in the Linux operating system.

How to Get the Absolute Path in Bash

An operating system’s building blocks are files and folders. Being Linux users, we operate several file and directory actions.

Finding a file’s entire path is one such process. A file’s complete path is also known as its absolute or canonical path.

An address used to find a file or folder is called a path. An absolute path is a complete address or location that allows us to retrieve the desired file from any location.

Meanwhile, a relative path is established about the active working directory.

A path is a reference to a directory or file. In Linux, there are two sorts of paths: absolute and relative.

The current working directory determines the relative path. On the other hand, the absolute path is the complete path to a file or directory.

There are several methods in Bash for getting a script’s whole directory. We can use realpath, readlink, or even write our small custom script.

Using a relative address makes getting the entire address very simple.

The whole path is supplied explicitly from the root directory. The / is an easy method to know which is which.

A relative path does not begin with /. (the root folder). This article will explain how to find a script’s absolute path in Bash.

This article will go over numerous methods for determining the entire path of a file or folder.

The Root Directory of a File in Linux

The file system’s topmost directory is Root Directory or Absolute Directory in Linux. The root directory is denoted with a slash /.

We can describe the location of any file or directory in the file system with a full path beginning with a slash /.

This full path is called the absolute path, which means it tells every step that must be taken from the root or the absolute beginning of the file system.

Use cd Command

The cd (change directory) command in the Linux operating system is used to alter the current working directory.

$ cd /tmp

Use mkdir Command

Then, we use the mkdir command followed by the name you want to give the new directory.

$ mkdir -p dir11/dir12/dir13/dir14/dir15

Use the touch Command to Create a Directory

After creating a new directory or directories, we will use the touch command for creating, changing, or modifying our files.

Example code:

$ touch dir11/dir12/file12.txt
$ touch dir11/dir12/dir13/dir14/file14.txt
$ touch dir11/dir12/dir13/dir14/dir15/file15.txt

The output of the code:

$ tree /tmp/dir11/
/tmp/dir11/
└── dir12
    ├── dir13
    │   └── dir14
    │       ├── dir15
    │       │   └── file15.txt
    │       └── file14.txt
    └── file12.txt

In the example above, we have created 4 directories and 3 files. Our absolute directory is dir11/dir12/dir13/dir14/dir15/file15.txt.

The Relative Path of a File in Linux

The absolute paths are easy to understand but can also be inconvenient to work with, especially while working with deeply nested directories.

This is where the relative path comes in handy. Moreover, looking at the code above, our relative directory path is dir13/file12.txt.

The readlink command can be used to obtain the complete address of a file. readlink is often used to capture the path of a symbolic link or a canonical file.

However, readlink can also compute the absolute path given a relative path. You must attach a flag to the readlink in all circumstances.

In such instances, the f flag is most commonly used.

The readlink command returns a list of canonical file names. To find the entire path of a file, we can utilize the -f argument of this command.

These are the directories, and there are files in these directories. We can get the absolute path of any file we need.

Example code:

$ cd /tmp/dir11/dir12/dir13/dir14/dir15/

We will write the file name we want to get the path of.

$ readlink -f file15.txt

After using the readlink -f with the name of the file, we have got the exact path of the file, and here it is:

/tmp/dir1/dir2/dir3/dir4/dir5/file5.txt

As we can see in the output of the code, we get the absolute path of the file we wanted.

Use the realpath Command to Get Absolute Path in Linux

As an alternative, we can use the realpath command to obtain a file’s absolute path in Linux.

Example code:

$ cd /tmp/dir11/dir12/dir13/dir14/
$ realpath file14.txt

Here is the path of the file file14.txt.

/tmp/dir1/dir2/dir3/dir4/file4.txt

As we can see, using the realpath command, we can get the desired absolute path of a file in Linux.

Use the find Command to Get Absolute Path in Linux

We can find a directory’s structure or hierarchy using the find command. In Linux, we can use the find command to print a file’s absolute path or location.

We will use the code below to find the absolute path of the file14.txt.

$ cd /tmp/dir11/
$ find $PWD -type f -name file14.txt

And the absolute path of the file file14.txt is:

/tmp/dir11/dir12/dir13/dir14/file14.txt

As we can see in the above output of the code, we get the absolute path of the file we wanted using the find command in the terminal.

We hope that you find this article helpful in understanding how to get the absolute path of any file in the Linux operating system.

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

LinkedIn