How to find all symbolic links pointing to any file/directory inside a given directory

You can find all the symbolic links using:

find / -type l 

you might want to run this as root in order to get to every place on the disc.

You can expand these using readlink -f to get the full path of the link and you should be able to grep the output against the target directory that you are considering for deletion:

find / -type l -exec readlink -f {} + | grep -F /dir2

Using find / -type l -printf '%l\n' doesn't work as you get relative links like ../tmp/xyz which might be pointing to your target dir, but are not matched because they are not fully expanded.


In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.

I worked around it using two -exec clauses:

find /home/ -type l -exec readlink -nf {} ';' -exec echo " -> {}" ';' | grep "/dir2"

Tags:

Symlink