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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/105212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:29:04  来源:igfitidea点击:

Recursively list all files in a directory including files in symlink directories

linux

提问by CuriousDawg

Suppose I have a directory /dirinside which there are 3 symlinks to other directories /dir/dir11, /dir/dir12, and /dir/dir13. I want to list all the files in dirincluding the ones in dir11, dir12and dir13.

假设我有一个目录/dir中,其中有3个符号连接其他目录 /dir/dir11/dir/dir12/dir/dir13。我想列出所有文件,dir包括dir11,dir12dir13.

To be more generic, I want to list all files including the ones in the directories which are symlinks. find ., ls -R, etc stop at the symlink without navigating into them to list further.

为了更通用,我想列出所有文件,包括目录中的符号链接文件。find ., ls -R, 等停在符号链接处,而无需导航到它们以进一步列出。

采纳答案by Michael Ridley

The -Loption to lswill accomplish what you want. It dereferences symbolic links.

将完成您想要的-L选项ls。它取消引用符号链接。

So your command would be:

所以你的命令是:

ls -LR

You can also accomplish this with

你也可以用

find -follow

The -followoption directs find to follow symbolic links to directories.

-follow选项指示 find 遵循指向目录的符号链接。

On Mac OS X use

在 Mac OS X 上使用

find -L

as -followhas been deprecated.

-follow已被弃用。

回答by pjz

Using ls:

使用 ls:

  ls -LR

from 'man ls':

来自'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

Or, using find:

或者,使用查找:

find -L .

From the find manpage:

从查找联机帮助页:

-L     Follow symbolic links.

If you find you want to only follow a fewsymbolic links (like maybe just the toplevel ones you mentioned), you should look at the -H option, which only follows symlinks that you pass to it on the commandline.

如果您发现只想跟踪几个符号链接(例如可能只是您提到的顶级链接),您应该查看 -H 选项,它只跟踪您在命令行中传递给它的符号链接。

回答by dvorak

find /dir -type f -follow -print

-type fmeans it will display real files (not symlinks)

-type f意味着它将显示真实文件(不是符号链接)

-followmeans it will follow your directory symlinks

-follow意味着它将遵循您的目录符号链接

-printwill cause it to display the filenames.

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

If you want a ls type display, you can do the following

如果你想要 ls 类型的显示,你可以执行以下操作

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

回答by Branan

ls -R -L

-Ldereferences symbolic links. This will also make it impossible to see any symlinks to files, though - they'll look like the pointed-to file.

-L取消引用符号链接。不过,这也将使您无法看到文件的任何符号链接 - 它们看起来像指向的文件。

回答by Steve Baker

How about tree? tree -lwill follow symlinks.

怎么样的树tree -l将遵循符号链接。

Disclaimer: I wrote this package.

免责声明:我写了这个包。

回答by Ashwin Muni

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.

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

链接指向的文件的属性,而不是来自链接本身(除非它是一个损坏的符号链接或 find 无法检查链接指向的文件)。使用此选项意味着 -noleaf。如果以后使用 -P 选项,-noleaf 仍然有效。如果 -L 有效并且 find 在其搜索过程中发现到子目录的符号链接,则将搜索符号链接指向的子目录。

回答by divinedragon

I knew treewas an appropriate, but I didn't have tree installed. So, I got a pretty close alternate here

我知道tree是合适的,但我没有安装树。所以,我在这里有一个非常接近的替代品

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

回答by qrtLs

in case you would like to print all file contents: find . -type f -exec cat {} +

如果您想打印所有文件内容find . -type f -exec cat {} +