如何查找并列出特定文件创建的所有符号链接?

我在不同的路径上创建了许多文件或目录的符号链接。我想要整个已创建符号链接路径(位置)的列表。
示例:
我在许多目录中为~/Pictures目录创建了符号链接。如何列出所有指向该~/Pictures目录的符号链接?
这可能吗?如果是,那么怎么做?

你需要进行彻底的搜索,因为没有像硬链接那样存储计数。可以参考其中一个答案使用find命令。 - ctrl-alt-delor
4个回答

这是一个例子:
find -L /dir/to/start -xtype l -samefile ~/Pictures

或者,也许更好的是:
find -L /dir/to/start -xtype l -samefile ~/Pictures 2>/dev/null

为了摆脱一些错误,如“Permission denied”、“Too many levels of symbolic links”或“File system loop detected”,以及其他情况下,“find”会抛出这些错误,当没有正确的权限或其他情况时。
-L - 跟随符号链接。
-xtype l - 文件是符号链接。
-samefile name - 文件引用与name相同的inode。当-L生效时,这可能包括符号链接。
注意:
在“-xtype l”中使用小写字母L,而不是数字1。
在macOS / Darwin上,“-xtype”是“-type”。

命令是否可以修改以查找包含路径的符号链接?例如,系统中可能链接到/Pictures/A、/ Pictures/A/B/C或~/Pictures子目录中的任何文件。 - snowbound

非常简单,只需使用选项-lname
find / -lname /path/to/original/dir

man find中找到的内容:
-lname pattern
       File is a symbolic link whose contents match shell pattern pattern.  The
       metacharacters do not treat `/' or `.' specially.  If the -L option or the
       -follow option is in effect, this test returns false unless the symbolic link
       is broken.

注意:请记住,符号链接可能存在于任何地方,包括远程系统(如果您共享文件),因此您可能无法找到所有符号链接。

请注意,如果符号链接是一个相对路径,比如 ../dir,那么它将无法找到绝对路径 /path/to/original/dir。您可以使用模式并排除错误的匹配项 -lname \*dir - Jason S

试试这个:
ls -i ~/

277566 Pictures

find . -follow -inum 277566(查找具有相同inode号的目录)

它将显示所有符号链接的路径。


4这将查找硬链接,而不是符号链接。硬链接共享inode号码。符号链接具有不同的inode号码。符号链接的inode具有路径而不是块列表。 - hildred
这个问题没有提及硬链接或软链接。 - nux