~ Useful Unix/Linux tricks and tips

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

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.