小编典典

如何在目录树中找到所有符号链接?

all

我正在尝试在我的网站的目录树中找到所有符号链接。我知道我可以find用来执行此操作,但我不知道如何递归检查目录。

我试过这个命令:

find /var/www/ -type l

’后来我发现里面的内容/var/www是符号链接,所以我把命令改成了:

find -L /var/www/ -type l

运行需要一段时间,但是我没有匹配。

我如何得到它来检查子目录?


阅读 100

收藏
2022-04-08

共1个答案

小编典典

这将递归遍历/path/to/folder目录并仅列出符号链接:

ls -lR /path/to/folder | grep ^l

如果您也打算使用符号链接,则应使用find命令,但应包含该-L选项;实际上find手册页说:

   -L     Follow symbolic links.  When find examines or prints information
          about files, the information used shall be taken from the  prop鈥�
          erties  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 subdirec鈥�
          tory pointed to by the symbolic link will be searched.

          When the -L option is in effect, the -type predicate will always
          match against the type of the file that a symbolic  link  points
          to rather than the link itself (unless the symbolic link is bro鈥�
          ken).  Using -L causes the -lname and -ilname predicates  always
          to return false.

然后试试这个:

find -L /var/www/ -type l

这可能会起作用:我在find手册页中找到了这个菱形:如果您正在使用该-type选项,则必须将其更改为该-xtype选项:

          l      symbolic link; this is never true if the -L option or the
                 -follow option is in effect, unless the symbolic link  is
                 broken.  If you want to search for symbolic links when -L
                 is in effect, use -xtype.

然后:

find -L /var/www/ -xtype l
2022-04-08