tags:

views:

112

answers:

4

I have two directories with the same list of files. I need to compare all the files present in both the directories using the diff command. Is there a simple command line option to do it, or do I have to write a shell script to get the file listing and then iterate through them?

Thanks!

+1  A: 

diff can not only compare two files, it can, by using the -r option, walk entire directory trees, recursively checking differences between subdirectories and files that occur at comparable points in each tree.

$ man diff

...

-r  --recursive
        Recursively compare any subdirectories found.

...
The MYYN
+1  A: 

Diff has an option -r which is meant to do just that.

diff -r dir1 dir2

Gautam Borad
+1  A: 

You can use the diff command for that:

diff -bur folder1/ folder2/

This will output a recursive diff that ignore spaces, with a unified context.

Laurent Etiemble
+1  A: 

If it's GNU diff then you should just be able to point it at the two directories and use the -r option.

Otherwise, try using

for i in $(\ls -d ./dir1/*); do diff ${i} dir2; done

N.B. As pointed out by Dennis in the comments section, you don't actually need to do the command substitution on the ls. I've been doing this for so long that I'm pretty much doing this on autopilot and substituting the command I need to get my list of files for comparison.

Also I forgot to add that I do '\ls' to temporarily disable my alias of ls to GNU ls so that I lose the colour formatting info from the listing returned by GNU ls.

Rob Wells
It's not necessary to use `ls`: `for i in ./dir1/*`
Dennis Williamson
@Dennis, funky shortcut. +1. I'm so used to doing command substitution though it's automatic and I just use which ever command I need, e.g. sometimes I'm using find. Using ls also lets me play with the the dir listing more, e.g. reverse time based instead of default sequence.
Rob Wells