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

LinuxBashSymlinkReverse

Linux Problem Overview


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!

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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

Solution 3 - Linux

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"
    fi
done

Solution 4 - Linux

Inspired by Gordon Davisson's comment. This is similar to another answer, but I got the desired results using exec. I needed something that could find symbolic links without knowing where the original file was located.

find / -type l -exec ls -al {} \; | grep -i "all_or_part_of_original_name"

Solution 5 - Linux

Here's what I came up with. I'm doing this on OS X, which doesn't have readlink -f, so I had to use a helper function to replace it. If you have it a proper readlink -f you can use that instead. Also, the use of while ... done < <(find ...) is not strictly needed in this case, a simple find ... | while ... done would work; but if you ever wanted to do something like set a variable inside the loop (like a count of matching files), the pipe version would fail because the while loop would run in a subshell. Finally, note that I use find ... -type l so the loop only executes on symlinks, not other types of files.

# Helper function 'cause my system doesn't have readlink -f
readlink-f() {
    orig_dir="$(pwd)"
    f="$1"
    while [[ -L "$f" ]]; do
        cd "$(dirname "$f")"
        f="$(readlink "$(basename "$f")")"
    done
    cd "$(dirname "$f")"
    printf "%s\n" "$(pwd)/$(basename "$f")"
    cd "$orig_dir"
}

target_file="$(readlink-f "$target_file")" # make sure target is normalized

while IFS= read -d '' linkfile; do
    if [[ "$(readlink-f "$linkfile")" == "$target_file" ]]; then 
        printf "%s\n" "$linkfile"
    fi
done < <(find "$search_dir" -type l -print0)

Solution 6 - Linux

This may be too simplistic for what you want to do, but I find it useful. it does Not answer your question literally, as it's not 'run on the original file', but it accomplishes the task. But, a lot more HDD access. And, it only works for 'soft' linked files which is majority of user linked files.

from the root of you data storage directory or users data directories, wherever symlinked 'files' to the orig.file may reside, run the find command:

# find -type l -ls |grep -i 'orig.file' 

or

# find /Starting/Search\ Path/ -type l -ls |grep -i '*orig*'

I would Normally use part of the name eg, '*orig*' to start, because we know users will rename (prefix) a simply named file with a more descriptive one like " Jan report from London _ orig.file.2015.01.21 " or something.

Note: I've Never gotten the -samefile option to work for me.

clean, simple, easy to remember

hope this helps Someone. Landis.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsdaauView Question on Stackoverflow
Solution 1 - LinuxDennis WilliamsonView Answer on Stackoverflow
Solution 2 - LinuxpaxdiabloView Answer on Stackoverflow
Solution 3 - LinuxjillesView Answer on Stackoverflow
Solution 4 - LinuxJerView Answer on Stackoverflow
Solution 5 - LinuxGordon DavissonView Answer on Stackoverflow
Solution 6 - LinuxLandis ReedView Answer on Stackoverflow