I'm trying to diff two directories using (well) diff. However, diff actually follows symlinks and compares differences between the targeted files. I can't seem to find a way to not have diff do this. Instead, what I would like to know is whether the targets are the same (or different for that matter). In other words, if the symlinks point to the same file, (in my opinion) they are the same, else they are different and that is all I want diff to tell me when it comes across symlinks during a recursive call.

I've tried looking online but I either find topics relating to dealing with symlinks alone or ignoring them altogether.


GNU diff v3.3 introduce option --no-dereference that does the trick. This option is described by diff --help but not mentioned on the man page.

(see also https://stackoverflow.com/a/32026096/301717 and https://unix.stackexchange.com/a/151837/72654)

| improve this answer | |

If you add lines like:


to a file .ignore-diff

then you can execute diff(1) like this:

diff -ur -X .ignore-diff

What I would suggest is to create a file of the ignores

find . -type l -exec stat '{}' '+' | grep 'File:' | sort | sed "s/^  File: \`//" | sed s/\'.*// >.ignore-diff

finds all symlinks in subdirectories, get the relevant line(s), sorts them(i like alphabetical things) and boils it down to a plain simple file path for the files that are actually symlinks. it finishes redirecting into .ignore-diff

of course, there is also the possibility of doing the .ignore and then doing that same search again, but taking off the last sed, and add a few more seds to take out all but the destinations.

find . -type l -exec stat '{}' '+' | grep 'File:' | sort | sed "s/^  File: \`//" | sed "s/.*-> \`//" | sed "s/'//g" | sed s/\`// > symlinks-dest

do that for each dir and then you could diff those two symlinks-dest files to compare the symlink destinations.

| improve this answer | |
  • Thanks PsychoData. Seems to be a reasonable solution (in theory). – Ash Sep 10 '13 at 6:47
  • Not sure how long find will take to execute on a directory as large as mine though. – Ash Sep 10 '13 at 6:59
  • There seems to be a problem with running diff using an 'ignore' file. After playing around with what you suggested, it turns out that adding something like <dir>/<file> to .ignore-diff doesn't work. Adding just <file> however does work. This is a problem. What if I have a file called <file> in a different sub directory somewhere which I don't want ignored? – Ash Sep 12 '13 at 6:42

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.