递归列出目录中的所有文件,包括符号链接目录中的文件

187
假设我有一个目录/dir,其中包含3个符号链接指向其他目录/dir/dir11/dir/dir12/dir/dir13。我想列出dir中的所有文件,包括dir11dir12dir13中的文件。 为了更加通用,我想列出所有文件,包括符号链接目录中的文件。find .ls -R等命令会在符号链接处停止而不会进入这些目录来列出更多内容。

可能是如何使用Linux `find`仅获取文件名?的重复问题。 - Eugen Konkov
8个回答

283
-L选项可实现您想要的功能,它会解引用符号链接。
因此,您的命令将是:
ls -LR

你也可以使用以下方法来实现此目的

find -follow
-follow 选项会指示 find 命令跟随符号链接到目录。在 Mac OS X 上使用该选项,请执行以下操作:
find -L

-follow 已被弃用。


16
在新版本的find命令中,-follow已经被-L选项所取代。 - pjz
@pjz:'-follow deprecated; use -L'有交叉引用吗?令我相当惊讶的是,我在POSIX / SUS标准的http://www.opengroup.org/onlinepubs/009695399/toc.htm中找到了“-L”和“-H”的列表,更令我惊讶的是没有“-follow”,所以我回答了自己的问题。 - Jonathan Leffler
这对我没用。一开始什么也没发生,然后我尝试使用“-follow”,它说找不到文件夹“ollow”。 - smatthewenglish
5
在 OS X 10.10 上,这个命令可以运行:find -L .。我遇到了和@S.Matthew_English一样的问题。 - fregante
ls -LR1 其中-1用于每行打印一个文件 - Bruce_Warrior
显示剩余2条评论

126

那么对于tree呢?tree -l会跟随符号链接。

免责声明:本人编写了此软件包。


2
那么一个Arch软件包怎么样? - Mr_and_Mrs_D
这是比 ls 更直观的视图。它也更容易粘贴。 - dhaupin
这是一个不错的界面,但有时候 tree 不可用,你无法将其添加到你正在尝试探索的系统中。 - thoroc

60
find /dir -type f -follow -print

-type f表示它将显示实际的文件(而不是符号链接)

-follow表示它将跟随您的目录符号链接

-print会导致它显示文件名。

如果您想要类似于ls的显示,可以执行以下操作:

find /dir -type f -follow -print|xargs ls -l

这将产生比 ls -L 选项更漂亮的显示。 - imbr

12

使用ls命令:

  ls -LR

来自 'man ls':

   -L, --dereference
          when showing file information for a symbolic link, show informa‐
          tion  for  the file the link references rather than for the link
          itself

或者,使用find:

find -L .

从find man页面:

-L     Follow symbolic links.

如果你发现你只想要跟随少量的符号链接(比如只是你提到的顶层链接),那么你可以看一下-H选项,它只会跟随你在命令行上传递给它的符号链接。


5
find -L /var/www/ -type l

# man find
-L     Follow  symbolic links.  When find examines or prints information about files, the information used shall be taken from the

properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched.


5

我知道tree是一个合适的工具,但我没有安装tree。因此,我在这里找到了一个相似的替代方案here

find ./ | sed -e 's/[^-][^\/]*\//--/g;s/--/ |-/'

这正是我所需要的。谢谢。 - Scott C

3
ls -R -L

-L 参数会解除符号链接。这也会使得任何指向文件的符号链接无法被查看,因为它们看起来像是被指向的文件。


-1

如果您想打印所有文件的内容

find . -type f -exec cat {} +

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接