We all know that you can use the pwd command on the command line to get the full path (absolute path) of the current directory:
pwd
So, how to get the absolute path of the file? There are several ways to print the full path of a file:
readlink
realpath
find
Use ls and pwd in combination
$ readlink -f sample.txt
/home/gliu/sample.txt
$ realpath -s sample.txt
/home/gliu/sample.txt
$ find $(pwd) -name sample.txt
/home/gliu/sample.txt
Let's introduce these commands in detail below. But before that, I suggest you first understand the basic knowledge related to the concepts of absolute path and relative path .
Use readlink to get the file path
The original use of readlink is to parse symbolic links, but we can use it to display the full path of a file, as follows:
readlink -f filename
The following is an example:
$ readlink -f sample.txt
/home/gliu/sample.txt
Use realpath to get the full path of a file
realpath was originally used to parse absolute file names. Here we can also use it to display the full path of the file:
realpath filename
Below is an example:
$ realpath sample.txt
/home/gliu/sample.txt
If using a symbolic link, it will show the actual path to the original file. You can force it not to follow symlinks (i.e. show the path to the current file):
realpath -s filename
Here's an example, by default it shows the full path to the source file, then I force it to show the symlink instead of the original file:
$ realpath linking-park
/home/gliu/Documents/ubuntu-commands.md
$ realpath -s linking-park
/home/gliu/linking-park
Use the find command to get the absolute path of a file
Here's how to get the file path using the find command.
In the find command, if the given path is a dot, then it will display the relative path; if the given path is an absolute path, then you can get the absolute path of the search file.
Use command placeholders with the find command, as follows:
find $(pwd) -name filename
We can get the absolute path of a single file using this:
$ find $(pwd) -name sample.txt
/home/gliu/sample.txt
Alternatively, you can use a matching pattern (such as an asterisk*) to get the paths to a set of files:
$ find $(pwd) -name "*.pdf"
/home/gliu/Documents/eBooks/think-like-a-programmer.pdf
/home/gliu/Documents/eBooks/linux-guide.pdf
/home/gliu/Documents/eBooks/absolute-open-bsd.pdf
/home/gliu/Documents/eBooks/theory-of-fun-for-game-design.pdf
/home/gliu/Documents/eBooks/Ubuntu 1804 english.pdf
/home/gliu/Documents/eBooks/computer_science_distilled_v1.4.pdf
/home/gliu/Documents/eBooks/the-art-of-debugging-with-gdb-and-eclipse.pdf
Use ls command to print the full path
Use the ls command to get the absolute path of the file, which is slightly more complicated.
We can use the environment variable PWD in the ls command to display the absolute paths of files and directories, as follows:
ls -ld $PWD/*
Using the above command, you will get the following output:
$ ls -ld $PWD/*
-r--rw-r-- 1 gliu gliu 0 Jul 27 16:57 /home/gliu/test/file2.txt
drwxrwxr-x 2 gliu gliu 4096 Aug 22 16:58 /home/gliu/test/new
To print the full path of a file using the above command, you can use it as follows:
ls -l $PWD/filename
This is not the best solution, but it works. See the example below:
$ ls -l $PWD/sample.txt
-rw-r--r-- 1 gliu gliu 12813 Sep 7 11:50 /home/gliu/sample.txt
Above we have introduced 4 ways to get the full path of a file in Linux, of which the find and ls commands are the most common, and the two methods realpath and readlink may be clear to many new users, but there is always a way to everything. It's always good to learn something new once in a while.
If you have any other ideas, please feel free to discuss them in the message area.