Comparing directories in local or remote hosts

I frequently need to compare two directories (with similar structures) to find different or new files. When I have to compare them locally, I use the diff command.

$ ls test*
file1 file2 file3 file4 file5
file1 file2 file3 file4 file5 file6

$ diff -qr test
Files test/file2 and differ
Files test/file4 and differ
Only in file6

The -q option tells diff to tell only whether the files differ and the -r option is for recursively comparing inside subdirectories.

A more complicated problem arises when the comparation has to be done remotely, that is one of the directories is in another host (Samba, NFS, etc is considered local) because diff does not support remote paths.

If we want to compare only one file between different hosts we can use the following command:

ssh user@remote_host "cat file.remote" | diff file.local -

But when one needs to compare directories, I use rsync because it can do something similar:

$ rsync -rvnc --delete test/ rafacas@rcmini:test/
building file list ... done
deleting file6

sent 191 bytes  received 24 bytes  61.43 bytes/sec
total size is 10  speedup is 0.05

This command shows what files rsync would have updated or deleted on the remote host. The -r option is for recursing into directories. The -v option increases verbosity. The -n option tells rsync to perform a “dry-run”, that is to make no changes on any host, and the -c option is for comparing based on checksum, instead of modification-time and size, which is the default (yes, I am a bit paranoid ;)

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields