How to compare the contents of two directories

This weekend I encountered a simple, but interesting topic, which concerns comparing the contents of two directories. The fun part is that you don't need any additional utilities as standard diff command is fully sufficient.

To compare contents of the first_directory and second_directory use the following command.

$ diff --brief --recursive first_directory second_directory

You reduce it a bit by using short options.

$ diff -qr first_directory second_directory

A sample output will look similar to the following.

Only in first_directory/_posts: 2014-09-02-how-to-compare-the-contents-of-two-directories.html
Only in first_directory: _site
Files first_directory/humans.txt and second_directory/humans.txt differ

You can also exclude specific files using the following syntax.

$ diff --brief --recursive --exclude *.html first_directory second_directory

Shorten previous command a bit.

$ diff -qr -x *.html first_directory second_directory

Now, sample output will not contain excluded files.

Only in first_directory: _site
Files first_directory/humans.txt and second_directory/humans.txt differ
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu