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

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

/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 -mR * | sed -n 's/://p'

解释:

ls -mR *列出以':'结尾的完整目录名,然后分别列出该目录下的文件

Sed -n 's/://p'查找以冒号结尾的行,去掉冒号并打印该行

通过遍历目录列表,我们也应该能够找到目录。还在努力中。通过xargs获取通配符是一个挑战。

其他回答

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

在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 | fgrep filename

在Mac OS 10.12.1中完美运行

ls -lR是你要找的,至少我是。干杯

除了ls,不使用任何外部命令:

ls -R1 /path | 
  while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done