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

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

    stat

    Absolute path of a single file:

    stat -c %n "$PWD"/foo/bar
    
    0 讨论(0)
  • 2020-11-28 17:42

    If you give the find command an absolute path, it will spit the results out with an absolute path. So, from the Ken directory if you were to type:

    find /home/ken/foo/ -name bar -print    
    

    (instead of the relative path find . -name bar -print)

    You should get:

    /home/ken/foo/bar
    

    Therefore, if you want an ls -l and have it return the absolute path, you can just tell the find command to execute an ls -l on whatever it finds.

    find /home/ken/foo -name bar -exec ls -l {} ;\ 
    

    NOTE: There is a space between {} and ;

    You'll get something like this:

    -rw-r--r--   1 ken admin       181 Jan 27 15:49 /home/ken/foo/bar
    

    If you aren't sure where the file is, you can always change the search location. As long as the search path starts with "/", you will get an absolute path in return. If you are searching a location (like /) where you are going to get a lot of permission denied errors, then I would recommend redirecting standard error so you can actually see the find results:

    find / -name bar -exec ls -l {} ;\ 2> /dev/null
    

    (2> is the syntax for the Borne and Bash shells, but will not work with the C shell. It may work in other shells too, but I only know for sure that it works in Bourne and Bash).

    0 讨论(0)
  • 2020-11-28 17:42
    lspwd() { for i in $@; do ls -d -1 $PWD/$i; done }
    
    0 讨论(0)
  • 2020-11-28 17:42

    This worked for me. But it didn't list in alphabetical order.

    find "$(pwd)" -maxdepth 1
    

    This command lists alphabetically as well as lists hidden files too.

    ls -d -1 "$PWD/".*; ls -d -1 "$PWD/"*;
    
    0 讨论(0)
  • 2020-11-28 17:42

    Recursive files can be listed by many ways in linus. Here i am sharing one liner script to clear all logs of files(only files) from /var/log/ directory and second check recently which logs file has made an entry.

    First:-

    find /var/log/ -type f  #listing file recursively 
    

    Second:-

    for i in $(find $PWD -type f) ; do cat /dev/null > "$i" ; done #empty files recursively 
    

    Third use:-

    ls -ltr $(find /var/log/ -type f ) # listing file used in recent
    

    note: for directory location you can also pass $PWD instead of /var/log.

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

    find / -print will do this

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