Home  >  Article  >  Operation and Maintenance  >  How to display the full path of a file in Linux

How to display the full path of a file in Linux

青灯夜游
青灯夜游Original
2023-02-14 10:07:1810992browse

Method to display the path: 1. Use the readlink command to obtain the file path, with the syntax "readlink -f filename"; 2. Use the realpath command to obtain the full path of the file, with the syntax "realpath filename"; 3. Use the find command Get the absolute path of the file with the syntax "find $(pwd) -name filename"; 4. Use the ls command to print the complete path with the syntax "ls -l $PWD/filename".

How to display the full path of a file in Linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

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 methods to print the full path of the file:

  • readlink
  • realpath
  • find
  • Used in combination with 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 introduce these commands in detail. 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 the file, as follows its syntax structure:

readlink -f filename

The following 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 parse absolute file names, here we You can also use it to show the full path to a file:

realpath filename

Here is an example:

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

If you use 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 is an example, by default it shows the full path to the source file, then I force it to show the symlinks instead Not 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

The following is how to use the find command to get the file path.

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 use this method to get the absolute path of a single file:

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

Alternatively, we can use a matching pattern (such as star *) 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

Use the ls command to print the complete path

Use the ls command to get the absolute path of the file, which has slightly more complicated advantages. 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 use the above command to print a certain The full path of a file can be used as follows:

ls -l $PWD/filename

Although this is not the best solution, it is very effective. See the following example:

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

Above we introduced how to obtain it in Linux There are 4 methods for the complete path of a file, among which the find and ls command methods are the most common, and the two methods realpath and readlink may be clear to many new users, but there is always a first time for everything, and you can learn new ones. Stuff is always good.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to display the full path of a file in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn