如何在Linux中生成带有绝对路径的文件列表? [英] How can I generate a list of files with their absolute path in Linux?
问题描述
我正在编写一个将文件路径作为输入的Shell脚本.
由于这个原因,我需要生成具有完整路径的递归文件列表.例如,文件bar
具有路径:
/home/ken/foo/bar
但是,据我所知,ls
和find
都只给出相对路径清单:
./foo/bar (from the folder ken)
这似乎是一个显而易见的要求,但是我在find
或ls
手册页中看不到任何内容.
如何在Shell中生成文件列表及其绝对路径?
如果为find
提供绝对路径,它将打印绝对路径.例如,要查找当前目录中的所有.htaccess文件:
find "$(pwd)" -name .htaccess
,或者如果您的外壳将$PWD
扩展到当前目录:
find "$PWD" -name .htaccess
find
只是将给定的路径添加到从该路径到文件的相对路径.
Greg Hewgill 还建议使用pwd -P
来解析当前目录中的符号链接.>
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 bar
has the path:
/home/ken/foo/bar
but, as far as I can see, both ls
and find
only give relative path listings:
./foo/bar (from the folder ken)
It seems like an obvious requirement, but I can't see anything in the find
or ls
man pages.
How can I generate a list of files in the shell including their absolute paths?
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.
这篇关于如何在Linux中生成带有绝对路径的文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!