symbolic link: find all files that link to this file

LinuxSymlink

Linux Problem Overview


Hallo all, I need to do this in linux:

  • Given: file name 'foo.txt'
  • Find: all files that are symbolic links to 'foo.txt'

How to do it? Thanks!

Linux Solutions


Solution 1 - Linux

It depends, if you are trying to find links to a specific file that is called foo.txt, then this is the only good way:

find -L / -samefile path/to/foo.txt

On the other hand, if you are just trying to find links to any file that happens to be named foo.txt, then something like

find / -lname foo.txt

or

find . -lname \*foo.txt # ignore leading pathname components

Solution 2 - Linux

Find the inode number of the file and then search for all files with the same inode number:

$ ls -i foo.txt
41525360 foo.txt

$ find . -follow -inum 41525360

Alternatively, try the lname option of find, but this won't work if you have relative symlinks e.g. a -> ../foo.txt

$ find . -lname /path/to/foo.txt

Solution 3 - Linux

I prefer to use the symlinks utility, which also is handy when searching for broken symlinks. Install by:

sudo apt install symlinks

Show all symlinks in current folder and subfolders:

symlinks -rv .
  • -r: recursive
  • -v: verbose (show all symlinks, not only broken ones)

To find a specific symlink, just grep:

symlinks -rv . | grep foo.txt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlukmacView Question on Stackoverflow
Solution 1 - LinuxDigitalRossView Answer on Stackoverflow
Solution 2 - LinuxdogbaneView Answer on Stackoverflow
Solution 3 - LinuxNikView Answer on Stackoverflow