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

后端 未结 21 2132
天涯浪人
天涯浪人 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:45

    Here's an example that prints out a list without an extra period and that also demonstrates how to search for a file match. Hope this helps:

    find . -type f -name "extr*" -exec echo `pwd`/{} \; | sed "s|\./||"
    
    0 讨论(0)
  • 2020-11-28 17:45

    Most if not all of the suggested methods result in paths that cannot be used directly in some other terminal command if the path contains spaces. Ideally the results will have slashes prepended. This works for me on macOS:

    find / -iname "*SEARCH TERM spaces are okay*" -print 2>&1  | grep -v denied |grep -v permitted |sed -E 's/\ /\\ /g'
    
    0 讨论(0)
  • 2020-11-28 17:47
    readlink -f filename 
    

    gives the full absolute path. but if the file is a symlink, u'll get the final resolved name.

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

    You can use

    find $PWD 
    

    in bash

    0 讨论(0)
  • 2020-11-28 17:47
    ls -1 | awk  -vpath=$PWD/ '{print path$1}'
    
    0 讨论(0)
  • 2020-11-28 17:50

    Use this for dirs (the / after ** is needed in bash to limit it to directories):

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

    this for files and directories directly under the current directory, whose names contain a .:

    ls -d -1 "$PWD/"*.*
    

    this for everything:

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

    Taken from here http://www.zsh.org/mla/users/2002/msg00033.html

    In bash, ** is recursive if you enable shopt -s globstar.

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