tags:

views:

2171

answers:

6

Before moving on to use SVN, I used to manage my project by simply keeping a /develop/ directory and editing and testing files there, then moving them to the /main/ directory. When I decided to move to SVN, I needed to be sure that the directories were indeed in sync.

So, what is a good way to write a shell script [ bash ] to recursively compare files with the same name in two different directories?

Note: The directory names used above are for sample only. I do not recommend storing your code in the top level :).

+4  A: 

The diff command has a -r option to recursively compare directories:

diff -r /develop /main
Greg Hewgill
Not technically bash, but a better solution if your diff supports it.
paxdiablo
Works for me! Thanks.Also, for prettier printing, diff -ru /develop /main also works
Animesh
A: 

[I read somewhere that answering your own questions is OK, so here goes :) ]

I tried this, and it worked pretty well

[/]$ cd /develop/
[/develop/]$ find | while read line; do diff -ruN "/main/$line" $line; done |less

You can choose to compare only specific files [e.g., only the .php ones] by editing the above line as

[/]$ cd /develop/
[/develop/]$ find -name "*.php" | while read line; do diff -ruN "/main/$line" $line; done |less

Any other ideas?

Animesh
+2  A: 

The diff I have available allows recursive differences:

diff -r main develop

But with a shell script:

( cd main ; find . -type f -exec diff {} ../develop/{} ';' )
paxdiablo
+1  A: 
diff -rqu /develop /main

It will only give you a summary of changes that way :)

If you want to see only new/missing files

diff -rqu /develop /main | grep "^Only

If you want to get them bare:

diff -rqu /develop /main | sed -rn "/^Only/s/^Only in (.+?): /\1/p"
Kent Fredric
A: 

The classic (System V Unix) answer would be dircmp dir1 dir2, which was a shell script that would list files found in either dir1 but not dir2 or in dir2 but not dir1 at the start (first page of output, from the pr command, so paginated with headings), followed by a comparison of each common file with an analysis (same, different, directory were the most common results).

This seems to be in the process of vanishing - I have an independent reimplementation of it available if you need it. It's not rocket science (cmp is your friend).

Jonathan Leffler
A: 

I have a related, but a bit harder question: Suppose you have reorganized the files in the two directories into possibly different subdirectories? What if you want to compare the files in one directory--and all those in its subdirectories--with another directory and its subdirectories?

As an example, suppose you were comparing two dirs, tmp1 and tmp2, that contain the following:

tmp1/
tmp1/something
tmp2/
tmp2/subdir/
tmp2/subdir/something

The script should compare the two copies of "something" and not worry about where they are. One twist is that the script should be able to handle file names like: "this file name has spaces in it"

You should post this as a new question with a link to the one above, saying something like "Related to the question found here:"
Dennis Williamson
Done! And thanks for the tip!
Well, I don't see it. Here's an abbreviated answer: `for file in tmp1/*; do echo -e "\n$file"; find tmp2 -name "$(basename "$file")" -print -exec diff "$file" {} \; ; done`
Dennis Williamson