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.
Article URL:https://www.jiyik.com/en/xwzj/opersys_9964.html
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u