Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am using diff -r a b to recursively compare directories a and b. It often happens though that there are some broken links (the same broken links in both a and b directories and to the same, non-existing targets).

diff then outputs error messages for those cases and exists with a non-zero exit code though I would like it to stay silent and exit with 0 as the directories are the same in my book.

How can I do that?

share|improve this question
    
Do you still want the symlinks compared (and identified as equivalent but broken), or is it acceptable to ignore all symlinks when doing this diff? –  ire_and_curses Sep 29 '12 at 23:04
    
compared and identified as equivalent, I don't care if they are broken. I am just trying to verify that my rsync worked. –  Marcus Junius Brutus Sep 29 '12 at 23:13

3 Answers 3

up vote 3 down vote accepted

Unfortunately, diff doesn't support ignoring symlinks:

Some files are neither directories nor regular files: they are unusual files like symbolic links, device special files, named pipes, and sockets. Currently, diff treats symbolic links like regular files; it treats other special files like regular files if they are specified at the top level, but simply reports their presence when comparing directories. This means that patch cannot represent changes to such files. For example, if you change which file a symbolic link points to, diff outputs the difference between the two files, instead of the change to the symbolic link.

diff should optionally report changes to special files specially, and patch should be extended to understand these extensions.

If all you want is to verify an rsync (and presumably fix what's missing), then you could just run the rsync command a second time. If you don't want to do that, then check-summing the directory may be sufficient.

If you really want to do this with diff, then you can use find to skip the symlinks, and run diff on each file individually. Pass your directories a and b in as arguments:

#!/bin/bash
# Skip files in $1 which are symlinks
for f in `find $1/* ! -type l`
do
    # Suppress details of differences
    diff -rq $f $2/${f##*/}
done

or as a one-liner:

for f in `find a/* ! -type l`;do diff -rq $f b/${f##*/};done

This will identify files that differ in content, or files which are in a but not in b.

Note that:

  • since we are skipping symlinks entirely, this won't notice if symlink names are not present in b. If you required that, you would need a second find pass to identify all the symlinks and then explicitly check for their existence in b.
  • Extra files in b will not be identified, since the list is constructed from the contents of a. This probably isn't a problem for your rsync scenario.
share|improve this answer
    
The proposed script does not work recursively for any directories present in directory 'a' (the paths created for 'b' using b/${f##*} are not correct). –  Marcus Junius Brutus Aug 28 at 11:37
    
@MarcusJuniusBrutus - Yes, you're right. I think the solution is to remove a #, e.g. for f in find a/* ! -type l;do echo $f b/${f#*/};done. I don't have time to test this right now though. Let me know if that works. –  ire_and_curses Aug 28 at 20:01
    
It is better however it still messes up the filepaths in many cases. The script (with a # removed) appears to need to be invoked from a directory directly over 'a' to work. –  Marcus Junius Brutus Aug 28 at 21:08

You can use a newer version of diff

The diff in GNU diffutils 3.3 includes a --no-dereference option that allows you to compare the symlinks themselves rather than their targets. It reports if they differ, is quiet if they agree and doesn't care whether they're broken.

I don't know when the option was added; it isn't present in 2.8.1.

share|improve this answer

Since version 3.3 GNU diff supports not dereferencing symlinks, but then compares the paths they point to.

Install GNU diffutils >= 3.3 and use the '--no-dereference' option; there is no short option for that.

Diagnostic will be silent if equal or:

Symbolic links /tmp/noderef/a/symlink and /tmp/noderef/b/symlink differ

share|improve this answer

Your Answer

 
discard

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.