查找传入的符号链接

jrd*_*oko 7 find symlink files

在 Linux 中,查找指向给定文件的所有符号链接的最佳方法是什么(无论符号链接是相对的还是绝对的)?我意识到这将需要扫描整个文件系统。

cam*_*amh 10

GNUfind-samefile测试。根据手册页:

-samefile name
    File refers to the same inode as name. When -L is in 
    effect, this can include symbolic links.
Run Code Online (Sandbox Code Playgroud)
$ find -L / -samefile /path/to/file
Run Code Online (Sandbox Code Playgroud)

这将找到指向 的所有链接/path/to/file,其中包括硬链接和文件本身。如果您只需要符号链接,则可以单独测试find( test -L)的结果。

您应该阅读它的影响-L是什么,并确保它不会给您的搜索带来任何问题。

注意:虽然文档说它寻找具有相同 inode 编号的文件,但它似乎可以跨文件系统工作。

例如 /home 和 /tmp 是独立的文件系统

$ touch ~/testfile
$ ln -s ~/testfile /tmp/foo
$ ln -s /tmp/foo /tmp/bar
$ mkdir /tmp/x
$ ln -s ~/testfile /tmp/x/baz
$ find -L /tmp -samefile ~/testfile
/tmp/bar
/tmp/foo
/tmp/x/baz
Run Code Online (Sandbox Code Playgroud)

注意这是如何返回 /tmp/bar 的,它是 /tmp/foo 的符号链接,它是 ~/testfile 的符号链接。如果您只想找到指向目标文件的直接符号链接,这将不起作用。