我如何吐出一行递归路径的平面列表?

例如,我只想要一个文件的完整路径的平面列表:

/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow
/home/dreftymac/stackoverflow/alpha.txt
/home/dreftymac/stackoverflow/bravo.txt
/home/dreftymac/stackoverflow/charlie.txt

ls -a1几乎满足了我的需要,但我不想要路径片段,我想要完整的路径。


当前回答

在ls目录的末尾添加通配符将强制执行全路径。现在你有这个:

$ ls /home/dreftymac/
foo.txt
bar.txt
stackoverflow
stackoverflow/alpha.txt
stackoverflow/bravo.txt
stackoverflow/charlie.txt

你可以这样做:

$ ls /home/dreftymac/*
/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow:
alpha.txt
bravo.txt
charlie.txt

不幸的是,这不会打印递归目录的完整路径,因此这可能不是您正在寻找的完整解决方案。

其他回答

一长串的答案。 这很有帮助,最后,我创建了我自己的,我正在寻找:

列出目录及其子目录下的所有文件。

find "$PWD" -type f

列出目录及其子目录下的所有目录。

find "$PWD" -type d

列出目录及其子目录下的所有目录和文件。

find "$PWD"

试试下面的简单方法:

find "$PWD"

我不知道完整的路径,但你可以用-R来递归。或者,如果您不倾向于ls,您可以只查找*。

realpath命令输出解析后的路径:

realpath *

为了包含点文件,将ls -a的输出管道到realpath:

ls -a | xargs realpath

递归列出子目录:

ls -aR | xargs realpath

如果文件名中有空格,man xargs建议使用-o选项来防止文件名被错误地处理,这与find -print0的输出效果最好,并且它开始看起来比其他答案复杂得多:

find -print0 |xargs -0 realpath

参见Unix和Linux的stackexchange问题如何列出一个目录中的所有文件与绝对路径。

拥有使用所有可能的ls选项的自由:

查找-type f | xargs ls -1