如何查找并列出为特定文件创建的所有符号链接?
我在特定文件或目录的各种路径上创建了许多符号链接。 我想要创建符号链接路径(位置)的完整列表。
例:
我在许多目录上为~/Pictures
目录创建了符号链接。 如何列出~/Pictures
目录中的所有符号链接?
那可能吗? 如果是,那怎么样?
这是一个例子:
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
它们find
抛出它们。
-
-L
– 遵循符号链接。 -
-xtype l
– 文件是符号链接 -
-samefile name
– File指的是与name
相同的inode。 当-L
生效时,这可以包括符号链接。
笔记:
- 在
-xtype l
使用小写L,而不是在数字1中使用。 - 在macOS / Darwin上,
-xtype
是-type
。
很简单,使用选项-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.
注意 :请记住,符号链接可以是任何位置,包括远程系统(如果您正在共享文件),因此您可能无法找到它们。
试试这个 :
ls -i ~/ 277566 Pictures
find . -follow -inum 277566
find . -follow -inum 277566
(查找具有相同inode编号的目录)
它将显示其所有符号链接路径。
我最喜欢这个单行:
find . -maxdepth 1 -type l -exec readlink -f '{}' \;
裁判:
https://unix.stackexchange.com/questions/21984/list-symlinks-in-current-directory