How to Compare Two Directories in Linux

If you’ve ever wanted to compare the contents of 2 directories using linux, you can leverage the “diff” command. Let’s go through an example to learn how.

We’ll be using the following “diff” flags so let’s understand what they do:
-q, –brief = report only when files differ
-r, –recursive = recursively compare any subdirectories found

So to get a listing of the different files from 2 directories, you’d issue this command.

diff -rq <dir1> <dir2>

In other words, to see all the differences between the directories /etc/nginx and /backup/etc/nginx, you’d issue the following command.

diff -rq /etc/nginx /backup/etc/nginx

Pretty cool, eh?