Find incoming symlinks
GNU find
has the -samefile
test. According to the man page:
-samefile name File refers to the same inode as name. When -L is in effect, this can include symbolic links.
$ find -L / -samefile /path/to/file
This will find all links to /path/to/file
, which includes hard links and the file itself. If you only want symlinks, you can individually test the results of find
(test -L
).
You should read up on what the effects of -L
are and ensure that it won't cause you any problems with your search.
Note: While the documentation says it looks for files with the same inode number, it does appear to work across filesystems.
e.g. /home and /tmp are separate filesystems
$ 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
Note how this is returning /tmp/bar, which is a symlink to /tmp/foo, which is a symlink to ~/testfile. If you only wanted to find direct symlinks to your target file, this won't work.