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

我试过这个命令:

find /var/www/ -type l

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

find -L /var/www/ -type l

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

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


当前回答

Find already默认递归查找:

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

其他回答

您可以安装“符号链接”包并使用实用程序

symlinks -rv "/path"

    -c == change absolute/messy links to relative
    -d == delete dangling links
    -o == warn about links across file systems
    -r == recurse into subdirs
    -s == shorten lengthy links (displayed in output only when -c not specified)
    -t == show what would be done by -c
    -v == verbose (show all symlinks)

悬着的链接是断裂的链接。

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

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

而如果你也想看到他们的目标文件,只需追加一个ls

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

Find already默认递归查找:

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

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

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

我所做的是在我的bin目录中创建一个脚本,就像一个别名。 例如,我有一个名为 迷幻药 Ls -l | grep ^d

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

只要chmod +x就可以了。