Symbolic link: find all files that link to this file

Hi everyone, I need to do this on linux:

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

How to do it? Thank!

+79
linux symlink
May 31 '11 at 8:26
source share
3 answers

It depends, if you are trying to find links to a specific file 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 called foo.txt , then something like

 find / -lname foo.txt 

or

 find . -lname \*foo.txt # ignore leading pathname components 
+95
May 31 '11 at 8:46 a.m.
source share
β€” -

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 find parameter, but this will not work if you have relative symlinks, for example. a -> ../foo.txt

 $ find . -lname /path/to/foo.txt 
+17
May 31 '11 at 8:41 a.m.
source share

I prefer to use the symlinks utility, which is also handy for finding broken symbolic links. Install by:

 sudo apt install symlinks 

Show all symbolic links in the current folder and subfolders:

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

To find a specific symlink, just grep :

 symlinks -rv . | grep foo.txt 
0
Aug 11 '17 at 13:57 on
source share



All Articles