如何查找目录树中的所有符号链接?

我试图在我的网站的目录树中找到所有的符号链接。 我知道我可以使用find来做到这一点,但我不知道如何递归检查目录。

我试过这个命令:

find /var/www/ -type l

...后来我发现/var/www中的内容是符号链接,所以我将命令更改为:

find -L /var/www/ -type l

它需要一段时间才能运行,但我没有找到匹配。

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


这将递归遍历/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手册页中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

一个命令,没有管道

find . -type l -ls

干净利落...

扩展这个答案,这里有几个符号链接相关的find命令:

找到指向特定目标的符号链接

find . -lname link_target

请注意, link_target可能包含通配符。

找到破碎的符号链接

find -L . -type l -ls

-L选项指示find遵循符号链接,除非破坏。

查找并替换断开的符号链接

find -L . -type l -delete -exec ln -s new_target {} ;

更多查找示例

更多find示例可以在这里找到:http://hamwaves.com/find/


默认情况下已经递归find

[15:21:53 ~]$ mkdir foo
[15:22:28 ~]$ cd foo
[15:22:31 ~/foo]$ mkdir bar
[15:22:35 ~/foo]$ cd bar
[15:22:36 ~/foo/bar]$ ln -s ../foo abc
[15:22:40 ~/foo/bar]$ cd ..
[15:22:47 ~/foo]$ ln -s foo abc
[15:22:52 ~/foo]$ find ./ -type l
.//abc
.//bar/abc
[15:22:57 ~/foo]$ 
链接地址: http://www.djcxy.com/p/78105.html

上一篇: How do I find all of the symlinks in a directory tree?

下一篇: Git Symlinks in Windows