Introduction to 4 Methods of Obtaining the Full Path of a File in Linux

We have introduced   4 methods to obtain the full path of a file in  Linux , among which the find and ls commands are the most common methods, and the two methods of realpath and readlink may be clear to many new users, but there is always a first for everything Again, it's always good to learn something new.

We all know that the pwd command can be used 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
  • Combination of ls and pwd
$ 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 take a closer look at these commands. But before that, I suggest to understand the basics of absolute path and relative path concepts.

Use readlink to get the file path

The original purpose of readlink is to resolve symbolic links, but we can use it to display the full path of the file, as its syntax structure is as follows:

readlink -f filename

Here is an example:

$ readlink -f sample.txt
/home/gliu/sample.txt

Use realpath to get the full path of the file

realpath was originally used to resolve absolute file names, here we can also use it to display the full path of a file:

realpath filename

Below is an example:

$ realpath sample.txt
/home/gliu/sample.txt

If a symlink is used, it will show the actual path of 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 the 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 is an absolute path, then the absolute path of the search file can be obtained. Use command placeholders with the find command, as follows:

find $(pwd) -name filename

We can use this method to get the absolute path of a single file:

$ find $(pwd) -name sample.txt
/home/gliu/sample.txt

Alternatively, you can use a matching pattern (such as an asterisk *) to get the path of 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

Print the full path with the ls command

Using the ls command to obtain the absolute path of the file is slightly more complicated. We can use the environment variable PWD in the ls command to display the absolute path 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 Jul Jul 0 Jul 27 16:57 /home/gliu/test/file2.txt
drwxrwxr-x 2 Jul Jul 4096 Aug 22 16:58 /home/glu/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 following example:

$ ls -l $PWD/sample.txt
-rw-r--r-- 1 Jul Jul 12813 Sep 7 11:50 /home/gliu/sample.txt

Above we have introduced 4 methods to obtain the full path of a file in Linux, among which the find and ls commands are the most common methods, and the two methods of realpath and readlink may be clear to many new users, but there is always a second way to everything. Once in a while, it's always good to learn something new.

Any other ideas are welcome to discuss.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/128172956