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.


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.

  • On some systems you may need to use readlink -f instead of -e. FreeBSD for example.
    – RobertL
    Dec 13 '15 at 21:09
  • 1
    What is the reason to quote the {} and the + ? Is it necessary in this context?
    – RobertL
    Dec 13 '15 at 21:17
  • @RobertL You're right, they don't seem to be necessary. I just put them there out of habit.
    – Joseph R.
    Dec 13 '15 at 21:28
  • readlink is not present on a standard UNIX system.
    – schily
    Dec 14 '15 at 12:56
  • @schily True. In that case you can use stat to find out the link's target.
    – Joseph R.
    Dec 14 '15 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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