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

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

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


当前回答

tar cf - $PWD|tar tvf -             

这是缓慢的,但递归地工作,并打印目录和文件。你可以用awk/grep管道它,如果你只是想要文件名,而不是所有其他的信息/目录:

tar cf - $PWD|tar tvf -|awk '{print $6}'|grep -v "/$"          

其他回答

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问题如何列出一个目录中的所有文件与绝对路径。

试试下面的简单方法:

find "$PWD"

我认为最好的方法是:

find -D tree /fullpath/to-dir/ 

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

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

运行bash命令,格式如下:

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

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

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

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

#!/bin/bash

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