How to find all symlinks to a file?

dasfex :

This is my ierarchy:

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?

oguz ismail :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=8383&siteId=1