Find all dirs/files that have symlinks pointing to them

directory-structuresymlink

I am in a situation where I want to delete some cruft, but there might be some symlinks pointing to the cruft, so I don't want to delete those targets.

Is there a command or some switches that will tell me which files and directories have symlinks pointing to them? In my case, I'm only concerned about files/dirs in the first level of a particular directory (i.e. no recursion), but a general command might be useful in the future.

Best Answer

A file does not keep track of the symbolic links pointing to it. Instead, search for symbolic links under your tree and find out which file/dir they point to using readlink:

find -type l -exec readlink -e -- "{}" \+ | sort | uniq

Since find's default behavior is recursion, this will work for arbitrary depth.

Related Question