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

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

/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 -R1 /path | 
  while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done

其他回答

运行bash命令,格式如下:

find /path -type f -exec ls -l \{\} \;

同样地,要去掉-l细节,只返回绝对路径:

find /path -type f -exec ls \{\} \;

找到/就行了

@ghostdog74: 对你的解决方案稍作调整。 以下代码可用于搜索文件的完整绝对路径。

sudo ls -R / | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }' | grep [file_to_search]

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

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

查找-type f | xargs ls -1