Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bunch of folders/files that are 10+ levels deep.

How can I find any symbolic links that point outside this folder tree?

I tried find -type l but this returns all soft links... even those whose destination is with in the folder tree.

Thank you

share|improve this question
up vote 2 down vote accepted

If all of the symbolic link targets are absolute, you can do something like this:

find /folder/tree -type l -not -lname '/folder/tree/*' -print

However, if you have any relative links in your tree, especially those with ./ or ../ embedded in the target paths, you'll probably need to loop through each one to normalize the target, and then see if it matches the folder tree:

find /folder/tree -type l -print | \
    while read symlink
        target=$(readlink -f "$symlink")
        expr match "$target" "^/folder/tree/.*" >/dev/null || echo "$symlink"
# end of pipeline

Both do the same thing, which is to print each symbolic link that has a target not matching /folder/tree.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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