[unix] Compare the contents of two directories in the command line

11 Jan

The first thing that comes to mind when you try to do any comparison, e.g. between two files or directories, is diff. Here is how it is used to compare two directories, dir-1 and dir-2:
$ diff /path_to_dir-1/ /path_to_dir-2/

But if the files are very big in size, diff may take quite a while.

Another useful utility is comm which is relatively fast for comparison of two files. It prints out the unique lines of file-1 in column 1, that of file-2 in column 2 and the common lines in column 3. The column 1, 2 and 3 may be suppressed by using the flags -1, -2 and -3, respectively. So, for example, if we want to see which files are common in dir-1 and dir-2, we may use comm with the “-1” and “-2” flags on the output of the ls command on the directories:
$ comm -1 -2 <(ls /path_to_dir-1/) <(ls /path_to_dir-2/)

Note: Of course, you can do a diff (or better vimdiff) of the outputs of ls as above, but comm still wins over diff/vimdiff because of the option to selectively output common or unique lines. Also notice that these forms are different from the diff mentioned at the top which does the comparison byte-by-byte.

About these ads
Leave a comment

Posted by on January 11, 2010 in cli, unix


Tags: , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: