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