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

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

/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几乎满足了我的需要,但我不想要路径片段,我想要完整的路径。


当前回答

我认为最好的方法是:

find -D tree /fullpath/to-dir/ 

(或者为了保存到TXT文件中)

find -D tree /fullpath/to-dir/ > file.txt

其他回答

从当前位置的所有文件递归列表:

Ls -l $(查找。类型)

@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]

不要把事情搞复杂。我只是使用这个,得到了一个漂亮的输出:

ls -lR /path/I/need

如果将目录作为相对路径传递,则需要在调用find之前将其转换为绝对路径。在下面的例子中,目录作为第一个参数传递给脚本:

#!/bin/bash

# get absolute path
directory=`cd $1; pwd`
# print out list of files and directories
find "$directory"

我认为最好的方法是:

find -D tree /fullpath/to-dir/ 

(或者为了保存到TXT文件中)

find -D tree /fullpath/to-dir/ > file.txt