Skip to main content

Linux Tips: Comparing Directories

Linux Performance
By Keith Edmunds3 May 2017No Comments

Sometimes we want to compare two directory trees – maybe one is from another system or a backup, and you want to see how they differ. The diff command has a couple of qualifiers that can help:

  • -r recurses subdirectories
  • -q only reports that files are different rather than displaying the differences.

Here’s a typical Apache configuration directory tree from a Debian-based system:

$ tree -d apache2
apache2
├── conf-available
├── conf.d
├── conf-enabled
├── mods-available
├── mods-enabled
├── sites-available
└── sites-enabled

In that directory tree, there are 218 files:

$ ls apache2 | wc -l
218

Let’s assume we have a backup of that directory in apache2-backup, and we want to see what has changed since that backup was taken. We want to see:

  • files that have been added
  • files that have been removed
  • files that have changed

We don’t need to look at all 218 file to find those differences. Instead, we can use diff:

$ diff -rq apache2 apache2-backup
Files apache2/apache2.conf and apache2-backup/apache2.conf differ
Only in apache2-backup/sites-enabled: wiki.example.com.conf

Could this Linux Tip be improved? Let us know in the comments below.