Home » Linux

How to Compare Directories

6 February 2011 No Comment

Have you ever used diff to compare two files in Linux? Yes, diff is a very powerful tool. But did you know that you can use it to compare two directories?

By using the -r option, you can traverse a directory tree and check the differences between files and directories. With the -q option, you can suppress the line-by-line comparison in files. For example,

diff -rq dirA dirB

list the files that exist in dirA and not dirB, and vice versa. It also lists the files that differ between dirA and dirB. To make the list more elegant, you can use grep and sort. For example,

diff -rq dirA dirB | grep -v -e ”DS_Store” -e ”Thumbs” | sort > diffs.txt

does the same thing as the last example but it also removes uninteresting files, sort the list and output it to a file called diffs.txt

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

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>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.