LINUX递归地列出一个目录中的所有文件,包括symlink目录中的文件

时间:2022-09-01 22:47:34

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

假设我有一个目录/dir,其中有3个指向其他目录/dir/dir11、/dir/dir12和/dir/dir13的符号链接。我要列出目录中的所有文件,包括dir11、dir12和dir13中的所有文件。

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.

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

7 个解决方案

#1


164  

The -L option to ls will accomplish what you want. It dereferences symbolic links.

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

So your command would be:

所以你的命令是:

ls -LR

You can also accomplish this with

您还可以使用

find -follow

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

-follow选项指示find跟踪到目录的符号链接。

On Mac OS X use

在Mac OS X上使用

find -L

as -follow has been deprecated.

as -follow已经被弃用了。

#2


98  

How about tree? tree -l will follow symlinks.

树怎么样?树-l将跟随符号链接。

Disclaimer: I wrote this package.

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

#3


35  

find /dir -type f -follow -print

-type f means it will display real files (not symlinks)

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

-follow means it will follow your directory symlinks

-跟随意味着它将跟随你的目录符号链接。

-print will cause it to display the filenames.

-打印将使它显示文件名。

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

如果希望显示ls类型,可以执行以下操作

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

#4


7  

Using ls:

使用ls:

  ls -LR

from 'man ls':

从“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 few symbolic 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选项,它只遵循您在命令行上传递给它的符号链接。

#5


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.

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

#6


2  

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

我知道树是合适的,但我没有安装树。这里我有一个很接近的选择

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

#7


2  

ls -R -L

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

- l取消引用符号链接。这也将使我们不可能看到任何指向文件的符号链接——它们看起来像指向文件。

#1


164  

The -L option to ls will accomplish what you want. It dereferences symbolic links.

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

So your command would be:

所以你的命令是:

ls -LR

You can also accomplish this with

您还可以使用

find -follow

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

-follow选项指示find跟踪到目录的符号链接。

On Mac OS X use

在Mac OS X上使用

find -L

as -follow has been deprecated.

as -follow已经被弃用了。

#2


98  

How about tree? tree -l will follow symlinks.

树怎么样?树-l将跟随符号链接。

Disclaimer: I wrote this package.

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

#3


35  

find /dir -type f -follow -print

-type f means it will display real files (not symlinks)

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

-follow means it will follow your directory symlinks

-跟随意味着它将跟随你的目录符号链接。

-print will cause it to display the filenames.

-打印将使它显示文件名。

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

如果希望显示ls类型,可以执行以下操作

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

#4


7  

Using ls:

使用ls:

  ls -LR

from 'man ls':

从“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 few symbolic 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选项,它只遵循您在命令行上传递给它的符号链接。

#5


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.

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

#6


2  

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

我知道树是合适的,但我没有安装树。这里我有一个很接近的选择

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

#7


2  

ls -R -L

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

- l取消引用符号链接。这也将使我们不可能看到任何指向文件的符号链接——它们看起来像指向文件。