1

This question already has an answer here:

How can I find all of the symbolic links in a directory, that point to the same target?

For example, here is a list files in a directory:

a.txt
b.txt
a -> target1
c -> target1
m -> target2
n -> target3
c.txt
z -> target1

I want to get a list of what linked to target1. So the expected output will be:

a
c
z

marked as duplicate by Jeff Schaller, sam, GAD3R, jayhendren, Eric Renouf Dec 29 '16 at 18:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • What would you want if there were recursive symlinks: t -> z? Would you want all four a c z t to be returned? – Jedi Dec 29 '16 at 10:33
  • Yes. All of them. – Omri Dec 29 '16 at 10:36
2

Consider the following directory structure:

lrwxrwxrwx [...] a -> target1
lrwxrwxrwx [...] c -> target1
lrwxrwxrwx [...] m -> target2
lrwxrwxrwx [...] n -> target3
lrwxrwxrwx [...] t -> z
-rw-r--r-- [...] target1
-rw-r--r-- [...] target2
-rw-r--r-- [...] target3
lrwxrwxrwx [...] z -> target1

Use GNU find to find all files pointing to the same inode:

find -L . -samefile target1
  • -L follows symlinks, even if you have recursive symlinks
  • -samefile finds all files which point to the same inode as the given paramter target1

The output in the above directory would look like this:

./z
./a
./target1
./c
./t

Not the answer you're looking for? Browse other questions tagged or ask your own question.