我试图为我的网站找到目录树中的所有符号链接。我知道我可以用find来做这个,但我不知道如何递归地检查目录。
我试过这个命令:
find /var/www/ -type l
后来我发现/var/www中的内容是符号链接,所以我将命令更改为:
find -L /var/www/ -type l
它需要一段时间来运行,但我没有得到匹配。
我如何得到这个检查子目录?
我试图为我的网站找到目录树中的所有符号链接。我知道我可以用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 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)
悬着的链接是断裂的链接。
请找到下面的一行bash脚本命令,在任何基于linux的操作系统中递归地找到所有损坏的符号链接
a=$(find / -type l); for i in $(echo $a); do file $i ; done |grep -i broken 2> /dev/null
这将递归遍历/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
要查看符号链接本身,可以使用
find -L /path/to/dir/ -xtype l
而如果你也想看到他们的目标文件,只需追加一个ls
find -L /path/to/dir/ -xtype l -exec ls -al {} \;