How can I generate a list of files with their absolute path in Linux?

后端 未结 21 2131
天涯浪人
天涯浪人 2020-11-28 16:56

I am writing a shell script that takes file paths as input.

For this reason, I need to generate recursive file listings with full paths. For example, the file

相关标签:
21条回答
  • 2020-11-28 17:23

    This will give the canonical path (will resolve symlinks): realpath FILENAME

    If you want canonical path to the symlink itself, then: realpath -s FILENAME

    0 讨论(0)
  • 2020-11-28 17:26
    ls -d "$PWD/"*
    

    This looks only in the current directory. It quotes "$PWD" in case it contains spaces.

    0 讨论(0)
  • 2020-11-28 17:29

    Command: ls -1 -d "$PWD/"*

    This will give the absolute paths of the file like below.

    [root@kubenode1 ssl]# ls -1 -d "$PWD/"*
    /etc/kubernetes/folder/file-test-config.txt
    /etc/kubernetes/folder/file-test.txt
    /etc/kubernetes/folder/file-client.txt
    
    0 讨论(0)
  • 2020-11-28 17:30

    Just an alternative to

    ls -d "$PWD/"* 
    

    to pinpoint that * is shell expansion, so

    echo "$PWD/"*
    

    would do the same (the drawback you cannot use -1 to separate by new lines, not spaces).

    0 讨论(0)
  • 2020-11-28 17:39

    fd

    Using fd (alternative to find), use the following syntax:

    fd . foo -a
    

    Where . is the search pattern and foo is the root directory.

    E.g. to list all files in etc recursively, run: fd . /etc -a.

    -a, --absolute-path Show absolute instead of relative paths

    0 讨论(0)
  • 2020-11-28 17:40

    If you give find an absolute path to start with, it will print absolute paths. For instance, to find all .htaccess files in the current directory:

    find "$(pwd)" -name .htaccess
    

    or if your shell expands $PWD to the current directory:

    find "$PWD" -name .htaccess
    

    find simply prepends the path it was given to a relative path to the file from that path.

    Greg Hewgill also suggested using pwd -P if you want to resolve symlinks in your current directory.

    0 讨论(0)
提交回复
热议问题