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

我试过这个命令:

find /var/www/ -type l

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

find -L /var/www/ -type l

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

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


当前回答

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

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

其他回答

一个命令,没有管道

find . -type l -ls

说明:从当前目录查找。所有-type link和list -ls的详细引用。 简单明了……

在这个答案的基础上,下面是几个与符号链接相关的查找命令:

查找到特定目标的符号链接

find . -lname link_target

注意,link_target是一个可能包含通配符的模式。

查找损坏的符号链接

find -L . -type l -ls

l选项指示find遵循符号链接,除非断开。

查找并替换损坏的符号链接

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

更多示例

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

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

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...

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

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

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

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

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

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

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

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)

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