Tuesday, March 4, 2008

Compare two directories using diff - BASH

$ ls dir1/
bin.sh convert.pl hex.cpp top.py

$ ls dir2/
bin.sh convert.pl touch.cpp

$ diff -r --brief dir1 dir2
Only in dir1: hex.cpp
Only in dir1: top.py
Only in dir2: touch.cpp

To copy the content of dir1 into dir2 without copying the files that are already exist and are the same, use rsync(faster, flexible replacement for rcp):

$ rsync -a dir1/ dir2/

Now comparing the two directories:

$ diff -r --brief dir1 dir2
Only in dir2: touch.cpp

Now doing a rsync of dir2 to dir1
$ rsync -a dir2/ dir1/

Now both the directories are the same, no difference.
$ diff -r --brief dir1 dir2


Unknown said...

From man pages of 'diff'

-q --brief
Output only whether files differ.

-r --recursive
Recursively compare any subdirectories found.

malcolm said...

rsync -auv dir1/ dir2/

Adding the u only updates a file if the source is newer than the destination.

Very very useful especially for backups.


Unknown said...

@blinkingeye, thanks for the tip :-)

twig said...

Just what I needed, quick and simple.

© Jadu Saikia www.UNIXCL.com