How to compare files with same names in two different directories using a shell script


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 :).

9/23/2008 8:15:57 AM

Accepted Answer

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

diff -r /develop /main
9/23/2008 8:21:57 AM

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"

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow