haf*_*huk 221 bash symlink find
我正在尝试在我的网站的目录树中找到所有符号链接.我知道我可以find
用来做这个,但我无法弄清楚如何递归检查目录.
我试过这个命令:
find /var/www/ -type l
Run Code Online (Sandbox Code Playgroud)
...后来我发现里面的内容/var/www
是符号链接,所以我把命令改为:
find -L /var/www/ -type l
Run Code Online (Sandbox Code Playgroud)
运行需要一段时间,但是我没有匹配.
如何检查子目录?
zta*_*013 271
这将递归遍历/path/to/folder
目录并仅列出符号链接:
ls -lR /path/to/folder | grep ^l
Run Code Online (Sandbox Code Playgroud)
如果你的意图也是遵循符号链接,你应该使用你的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.
Run Code Online (Sandbox Code Playgroud)
然后尝试这个:
find -L /var/www/ -type l
Run Code Online (Sandbox Code Playgroud)
这可能会有效:我在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.
Run Code Online (Sandbox Code Playgroud)
然后:
find -L /var/www/ -xtype l
Run Code Online (Sandbox Code Playgroud)
Ser*_*ndt 200
find . -type l -ls
Run Code Online (Sandbox Code Playgroud)
干净利落...
扩展这个答案,这里有一些更符号链接相关的find
命令:
find . -lname link_target
Run Code Online (Sandbox Code Playgroud)
请注意,这.
是一个可能包含通配符的模式.
find -L . -type l -ls
Run Code Online (Sandbox Code Playgroud)
该-type l
选项指示-ls
遵循符号链接,除非它被破坏.
find -L . -type l -delete -exec ln -s new_target {} \;
Run Code Online (Sandbox Code Playgroud)
更多find
示例可以在这里找到:https://hamwaves.com/find/
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]$
Run Code Online (Sandbox Code Playgroud)
要查看符号链接本身,您可以使用
find -L /path/to/dir/ -xtype l
Run Code Online (Sandbox Code Playgroud)
如果你想看到他们目标的文件,只需附加一个ls
find -L /path/to/dir/ -xtype l -exec ls -al {} \;
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我发现的最好的事情-递归地向您显示当前目录中的符号链接,但不遵循它们,而是显示完整路径和其他信息:
find ./ -type l -print0 | xargs -0 ls -plah
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:
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...
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
268097 次 |
最近记录: |