Getting the absolute path in Bash
In this Bash article, we will learn different ways to get the absolute path in Linux. We will also learn some different Linux commands to get the absolute path of a file.
Before we begin, we need to understand the basic concepts of absolute and relative paths in Linux operating system.
How to get the absolute path in Bash
The building blocks of an operating system are files and folders. As Linux users, we operate multiple file and directory operations.
Finding the full path of a file is one such process. The full path of a file is also called its absolute path or canonical path.
The 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 required file from any location.
Also, create a relative path to the active working directory.
A path is a reference to a directory or file. In Linux, there are two kinds of paths: absolute and relative.
The current working directory determines the relative path. On the other hand, an absolute path is the complete path to a file or directory.
There are several ways in Bash to get the full address of a script. We can use realpath, readlink, or even write our own little custom script.
Using relative addresses makes it very easy to get the entire address.
The entire path is provided explicitly from the root directory. /
is an easy way to know which is which.
A relative path does not /
start with (root folder). This article will explain how to find the absolute path of a script in Bash.
This article describes several methods for determining the complete path to a file or folder.
Root directory of files in Linux
The topmost directory of the file system is the Root Directory or Absolute Directory in Linux. The root directory is represented by a slash /.
We can use /
a full path beginning with a slash to describe the location of any file or directory in the file system.
This complete path is called an absolute path, which means it tells every step that must be performed starting from the root or absolute beginning of the file system.
Using the cd Command
The cd (change directory) command in Linux operating system is used to change the current working directory.
$ cd /tmp
Using the mkdir command
We then use the mkdir command followed by the name you want to give the new directory.
$ mkdir -p dir11/dir12/dir13/dir14/dir15
Create a directory using the touch command
After creating one or more new directories, we will use the touch command to create, change, or modify our files.
Sample code:
$ touch dir11/dir12/file12.txt
$ touch dir11/dir12/dir13/dir14/file14.txt
$ touch dir11/dir12/dir13/dir14/dir15/file15.txt
Output of the code:
$ tree /tmp/dir11/
/tmp/dir11/
└── dir12
├── dir13
│ └── dir14
│ ├── dir15
│ │ └── file15.txt
│ └── file14.txt
└── file12.txt
In the above example, we created 4 directories and 3 files. Our absolute directory is dir11/dir12/dir13/dir14/dir15/file15.txt .
Relative paths to files in Linux
Absolute paths are easy to understand, but can be inconvenient to use, especially when dealing with deeply nested directories.
This is where relative paths come in handy. Also, looking at the code above, our relative directory path is dir13/file12.txt.
Get the absolute path using readlink command in Linux
readlink
The readlink command can be used to obtain the full address of a file. readlink is often used to capture the path of a symbolic link or a canonical file.
However, readlink
it is also possible to compute an absolute path given a relative path. In any case, you have to append the flag to the reading link.
In this case, the f flag is most often used.
readlink
The command returns a list of canonical file names. To find the full path of a file, we can use the -f parameter of this command.
These are directories, and within these directories are files. We can get the absolute path to any file we need.
Sample code:
$ cd /tmp/dir11/dir12/dir13/dir14/dir15/
We will write the name of the file we want to get the path for.
$ readlink -f file15.txt
After using with the file name readlink -f
, we get the exact path to the file, 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 to the file we wanted.
Get the absolute path using realpath command in Linux
As an alternative, we can use realpath command to get the absolute path of a file in Linux.
Sample code:
$ cd /tmp/dir11/dir12/dir13/dir14/
$ realpath file14.txt
This is the path to the file file14.txt.
/tmp/dir1/dir2/dir3/dir4/file4.txt
As we can see, using realpath command, we can get the required absolute path of a file in Linux.
Get the absolute path using find command in Linux
We can find the structure or hierarchy of a directory using the find command. In Linux, we can use the find command to print the absolute path or location of a file.
We will use the following code to find the absolute path of file14.txt.
$ cd /tmp/dir11/
$ find $PWD -type f -name file14.txt
The absolute path of the file file14.txt is:
/tmp/dir11/dir12/dir13/dir14/file14.txt
As we can see in the above code output, we got the absolute path of the file we wanted using the find command in the terminal.
We hope you found this article helpful in understanding how to get the absolute path of any file in Linux operating system.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Difference between Bash Nohup and &
Publish Date:2025/03/21 Views:151 Category:OPERATING SYSTEM
-
This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux Processes in the Backgr
Renaming Files in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
With the help of Bash scripts, you can automate your tasks. File renaming is a common task on various systems. You can rename all the files manually. However, if your file names have a sequence, it is better to automate this task. This way
Open Emacs in Bash
Publish Date:2025/03/21 Views:141 Category:OPERATING SYSTEM
-
This article will show you how to open Emacs from within Bash. We will also discuss how to install the Emacs text editor. Install EMACS in your system Suppose you don't have Emacs in your system. You can easily install it in your system wit
Clear the Terminal Screen in Bash
Publish Date:2025/03/21 Views:145 Category:OPERATING SYSTEM
-
There are various ways to clear the terminal in bash script. This article will discuss 3 methods to clear the terminal. Use tput reset to clear the terminal screen. The first method uses the keyword tput reset to clear the screen. When your
Reload .bash_profile from the command line
Publish Date:2025/03/21 Views:67 Category:OPERATING SYSTEM
-
In the shell, .bash_profile is used to customize the configuration of user settings. It is stored in the root directory or home directory and is mostly hidden from other users. This file holds all the configuration of the shell and is also
Check if input parameter exists in Bash
Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM
-
When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh
Getting optional input arguments in Bash
Publish Date:2025/03/21 Views:180 Category:OPERATING SYSTEM
-
Sometimes we need to create a dynamic function which can be executed in both without passing any arguments or without passing any arguments. For this we need to set some default values for those arguments so that if the arguments are
Using Double and Single Pipes in Bash
Publish Date:2025/03/21 Views:136 Category:OPERATING SYSTEM
-
In Bash, the double pipe || is also known as the OR operator in other programming languages. On the other hand, the single pipe | is known as a pipeline. In this article, we will see how to use double pipe (also known as OR) and pipelines i
Breaking out of a loop in Bash
Publish Date:2025/03/21 Views:131 Category:OPERATING SYSTEM
-
Using loops is a common task in any programming or scripting language. While using a loop, sometimes we need to stop it under a predefined condition. Like other programming and scripting languages, Bash uses the keyword break to stop any lo