Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: Find all symlinks of a given 'original' file? (reverse 'readlink')

Consider the following command line snippet:

$ cd /tmp/ $ mkdir dirA $ mkdir dirB $ echo "the contents of the 'original' file" > orig.file $ ls -la orig.file  -rw-r--r-- 1 $USER $USER 36 2010-12-26 00:57 orig.file  # create symlinks in dirA and dirB that point to /tmp/orig.file:  $ ln -s $(pwd)/orig.file $(pwd)/dirA/ $ ln -s $(pwd)/orig.file $(pwd)/dirB/lorig.file $ ls -la dirA/ dirB/ dirA/: total 44 drwxr-xr-x  2 $USER $USER  4096 2010-12-26 00:57 . drwxrwxrwt 20 root          root          36864 2010-12-26 00:57 .. lrwxrwxrwx  1 $USER $USER    14 2010-12-26 00:57 orig.file -> /tmp/orig.file  dirB/: total 44 drwxr-xr-x  2 $USER $USER  4096 2010-12-26 00:58 . drwxrwxrwt 20 root          root          36864 2010-12-26 00:57 .. lrwxrwxrwx  1 $USER $USER    14 2010-12-26 00:58 lorig.file -> /tmp/orig.file 

At this point, I can use readlink to see what is the 'original' (well, I guess the usual term here is either 'target' or 'source', but those in my mind can be opposite concepts as well, so I'll just call it 'original') file of the symlinks, i.e.

$ readlink -f dirA/orig.file  /tmp/orig.file $ readlink -f dirB/lorig.file  /tmp/orig.file 

... However, what I'd like to know is - is there a command I could run on the 'original' file, and find all the symlinks that point to it? In other words, something like (pseudo):

$ getsymlinks /tmp/orig.file /tmp/dirA/orig.file  /tmp/dirB/lorig.file 

Thanks in advance for any comments,

Cheers!

like image 1000
sdaau Avatar asked Dec 26 '10 00:12

sdaau


1 Answers

Using GNU find, this will find the files that are hard linked or symlinked to a file:

find -L /dir/to/start -samefile /tmp/orig.file 
like image 57
Dennis Williamson Avatar answered Sep 18 '22 09:09

Dennis Williamson