我正试图查找我的网站目录树中的所有符号链接。我知道可以使用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
手册中找到了这个钻石:如果你正在使用-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
解释: 从当前目录 .
开始,查找所有的 -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/
find
命令都有一个 -ls
选项。因此,find . -type l -ls
应该等同于上面的命令。 - Bratchleyfind -lname '*/dir/*' -printf '%P -> %l\n'
。值得一提的是,link_target是一个模式。 - x-yurifind
默认已经具备递归查找功能:
[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]$
find
默认会递归这些吗?我猜不会 ;) - hafichuk如果您只想查看符号链接本身,可以使用以下命令:
find -L /path/to/dir/ -xtype l
如果你想看它们目标的文件,只需添加ls即可。
find -L /path/to/dir/ -xtype l -exec ls -al {} \;
这是我迄今为止发现的最好的东西 - 它可以递归地显示当前目录中的符号链接,但不会跟随它们,以完整路径和其他信息显示:
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...
fd . -t l --print0 | xargs -0 ls -plah
。 - OldBuildingAndLoana=$(find / -type l); for i in $(echo $a); do file $i ; done |grep -i broken 2> /dev/null
find / -type l -exec file {} \; | grep -i broken 2>/dev/null
。请注意,您可能会得到很多错误的结果。 - ingroxdsymlinks -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 /var/www/ -xtype l
)。在你的find
命令中使用-xtype
而不是-type
应该能解决问题。祝你好运! - ztank1013ls -laR /path/to/folder | grep ^l
。 - wimvds-xtype
参数不可用。使用find . -type l
命令似乎会进行递归检查。 - amertkara