- How to Get the Absolute Path in Bash
- The Root Directory of a File in Linux
readlinkCommand to Get Absolute Path in Linux
realpathCommand to Get Absolute Path in Linux
findCommand to Get Absolute Path in Linux
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
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
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.
cd (change directory) command in the Linux operating system is used to alter the current working directory.
$ cd /tmp
Then, we use the
mkdir command followed by the name you want to give the new directory.
$ mkdir -p dir11/dir12/dir13/dir14/dir15
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.
$ 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
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
readlink Command to Get Absolute Path in Linux
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.
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.
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.
$ 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:
As we can see in the output of the code, we get the absolute path of the file we wanted.
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.
$ cd /tmp/dir11/dir12/dir13/dir14/ $ realpath file14.txt
Here is the path of the file
As we can see, using the
realpath command, we can get the desired absolute path of a file in Linux.
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
$ cd /tmp/dir11/ $ find $PWD -type f -name file14.txt
And the absolute path of the file
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.