bash 如何在目录树中找到所有符号链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8513133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find all of the symlinks in a directory tree?
提问by hafichuk
I'm trying to find all of the symlinks within a directory tree for my website. I know that I can use find
to do this but I can't figure out how to recursively check the directories.
我正在尝试在我的网站的目录树中查找所有符号链接。我知道我可以find
用来做到这一点,但我不知道如何递归检查目录。
I've tried this command:
我试过这个命令:
find /var/www/ -type l
… and later I discovered that the contents in /var/www
are symlinks, so I've changed the command to:
……后来我发现里面的内容/var/www
是符号链接,所以我把命令改为:
find -L /var/www/ -type l
it take a while to run, however I'm getting no matches.
运行需要一段时间,但是我没有匹配到。
How do I get this to check subdirectories?
我如何得到它来检查子目录?
回答by ztank1013
This will recursively traverse the /path/to/folder
directory and list only the symbolic links:
这将递归遍历/path/to/folder
目录并仅列出符号链接:
ls -lR /path/to/folder | grep ^l
If your intention is to follow the symbolic links too, you should use your find
command but you should include the -L
option; in fact the find
man page says:
如果您也打算遵循符号链接,则应使用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.
Then try this:
然后试试这个:
find -L /var/www/ -type l
This will probably work: I found in the find
man page this diamond: if you are using the -type
option you have to change it to the -xtype
option:
这可能会起作用:我在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.
Then:
然后:
find -L /var/www/ -xtype l
回答by Serge Stroobandt
One command, no pipes
一个命令,没有管道
find . -type l -ls
Explanation:find
from the current directory .
onwards all references of -type l
ink and list -ls
those in detail.
Plain and simple...
说明:find
从当前目录.
开始,所有对-type l
ink的引用,并-ls
详细列出。干净利落...
Expanding upon this answer, here are a couple more symbolic link related find
commands:
扩展这个答案,这里有几个与符号链接相关的find
命令:
Find symbolic links to a specific target
查找特定目标的符号链接
find . -lname link_target
Note that link_target
is a pattern that may contain wildcard characters.
请注意,这link_target
是一个可能包含通配符的模式。
Find broken symbolic links
查找损坏的符号链接
find -L . -type l -ls
The -L
option instructs find
to follow symbolic links, unless when broken.
该-L
选项指示find
遵循符号链接,除非损坏。
Find & replace broken symbolic links
查找和替换损坏的符号链接
find -L . -type l -delete -exec ln -s new_target {} \;
More find examples
更多查找示例
More find
examples can be found here: https://hamwaves.com/find/
更多find
例子可以在这里找到:https: //hamwaves.com/find/
回答by jman
find
already looks recursively by default:
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]$
回答by MariusPontmercy
To see just the symlinks themselves, you can use
要仅查看符号链接本身,您可以使用
find -L /path/to/dir/ -xtype l
while if you want to see also which files they target, just append an ls
而如果您还想查看它们的目标文件,只需附加一个 ls
find -L /path/to/dir/ -xtype l -exec ls -al {} \;
回答by siliconrockstar
This is the best thing I've found so far - shows you the symlinks in the current directory, recursively, but without following them, displayed with full paths and other information:
这是迄今为止我发现的最好的事情 - 向您显示当前目录中的符号链接,递归,但不遵循它们,显示完整路径和其他信息:
find ./ -type l -print0 | xargs -0 ls -plah
outputs looks about like this:
输出看起来像这样:
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...
回答by Alex M
What I do is create a script in my bin directory that is like an alias. For example I have a script named lsd ls -l | grep ^d
我所做的是在我的 bin 目录中创建一个类似于别名的脚本。例如,我有一个名为 lsd ls -l | 的脚本。grep ^d
you could make one lsl ls -lR | grep ^l
你可以做一个 lsl ls -lR | grep ^l
Just chmod them +x and you are good to go.
只需 chmod +x 就可以了。