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

时间:2011-12-14 23:19:48

标签: bash find symlink

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

我试过这个命令:

find /var/www/ -type l

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

find -L /var/www/ -type l

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

如何检查子目录?

7 个答案:

答案 0 :(得分:258)

这将以递归方式遍历/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

答案 1 :(得分:187)

一个命令,没有管道

find . -type l -ls

说明: 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示例可在此处找到:https://hamwaves.com/find/

答案 2 :(得分:9)

默认情况下,

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]$ 

答案 3 :(得分:6)

要查看符号链接本身,可以使用

find -L /path/to/dir/ -xtype l 

如果你想看到他们定位的文件,只需附加一个ls

find -L /path/to/dir/ -xtype l -exec ls -al {} \;

答案 4 :(得分:4)

这是我迄今为止发现的最好的东西 - 以递归的方式显示当前目录中的符号链接,但没有关注它们,显示完整路径和其他信息:

find ./ -type l -print0 | xargs -0 ls -plah

输出看起来像这样:

lrwxrwxrwx 1 apache develop 99 Dec  5 12:49 ./dir/dir2/symlink1 -> /dir3/symlinkTarget
lrwxrwxrwx 1 apache develop 81 Jan 10 14:02 ./dir1/dir2/dir4/symlink2 -> /dir5/whatever/symlink2Target
etc...

答案 5 :(得分:0)

我要做的是在我的bin目录中创建一个脚本,就像一个别名。 例如,我有一个名为             资讯系统             ls -l | grep ^ d

你可以做一个             lsl             ls -lR | grep ^ l

只需将其+ x chmod,您就可以开始使用

答案 6 :(得分:0)

请在下面找到一个 liner bash 脚本命令,以便在任何基于 linux 的操作系统中递归查找所有损坏的符号链接

a=$(find / -type l); for i in $(echo $a); do file $i ; done |grep -i broken 2> /dev/null