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

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


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


I've not seen a command for this and it's not an easy task, since the target file contains zero information on what source files point to it.

This is similar to "hard" links but at least those are always on the same file system so you can do a find -inode to list them. Soft links are more problematic since they can cross file systems.

I think what you're going to have to do is basically perform an ls -al on every file in your entire hierarchy and use grep to search for -> /path/to/target/file.

For example, here's one I ran on my system (formatted for readability - those last two lines are actually on one line in the real output):

pax$ find / -exec ls -ald {} ';' 2>/dev/null | grep '\-> /usr/share/applications'lrwxrwxrwx 1 pax pax 23 2010-06-12 14:56 /home/pax/applications_usr_share                                         -> /usr/share/applications


Symlinks do not track what is pointing to a given destination, so you cannot do better than checking each symlink to see if it points to the desired destination, such as

for i in *; do    if [ -L "$i" ] && [ "$i" -ef /tmp/orig.file ]; then        printf "Found: %s\n" "$i"    fidone