Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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 two folders containing a number of broken symlinks.

Is there a way to make diff compare the target of the links instead of trying to follow the links?

share|improve this question
I believe there's no option to have it prevent dereferencing the link. First hit on Google shows me the feature request with a proposed patch on the diffutils mailing list: diff: support for --no-dereference option – gertvdijk Jan 28 '13 at 14:06

With 'content of the links' I assume you mean 'target of the link'. If that's correct, then yes, there is. Sort of. And you have to get creatie with shell redirection :)

diff -u \
<(cd /path/to/dir1 && find . -type l -exec stat '{}' '+' | grep 'File:' | sort) \
<(cd /path/to/dir2 && find . -type l -exec stat '{}' '+' | grep 'File:' | sort)

What this does is run diff with as input the output of two commands:

cd /path/to/dir1 && find . -type l -exec stat '{}' '+' | grep 'File:' | sort
cd /path/to/dir2 && find . -type l -exec stat '{}' '+' | grep 'File:' | sort

These find | grep | sort commands display the name and target of all symlinks it can find in dir1 and dir2, together with their targets. So the net result is that you'll see which symlinks are different.

(Edited from the original answer, which included the full path to the filename in the output of ls, and therefore showed every file as different. Worked around this by using cd first. Also if the symlinks had different dates and times, diff would show them as different too. Worked around that by using stat to extract the information that we actually want.)

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.