Skip to content
Advertisement

How to find all symlinks to a file?

This is my hierarchy:

aaaaaaaa
|_q
  |_a.txt
|_w
  |_l1
  |_l2

l1 and l2 are symlinks to a.txt. I run this code to find all symlinks to a.txt in the /aaaaaaaa:

find ~/aaaaaaaa/ -exec ls -a {} ';' | grep '/home/khodor/aaaaaaaa/q/a.txt'

And it obviously doesn’t work, cause I must compare realpath of file with path of a.txt. In what way I should do this?

Advertisement

Answer

If you have GNU/BSD find just use -samefile primary.

$ find -L ~/aaaaaaaa/ -samefile ~/aaaaaaaa/q/a.txt 
/home/oguz/aaaaaaaa/q/a.txt
/home/oguz/aaaaaaaa/w/l2
/home/oguz/aaaaaaaa/w/l1
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement