28 8 / 2011

Comparing directory contents

Recently I had a hard drive fail on me, so spent quite a bit of time recovering the data to put onto a new drive. During this process, I also decided to retire my Windows Server 2008 box in favour of an Ubuntu Server 11.04.

Doing so meant that it was time to ditch my NTFS drives and replace them with ext4. To make sure that I don’t lose any data (failed copy, missed directory etc), before blowing away the backup, I needed to find a way to verify my backup matched my final content. Here’s the process in a couple of quick commands:

First, I list the contents of the directories and pipe them to a file

> ls -R /media/eps1 > /tmp/eps1
> ls -R /media/movies1/eps1 > /tmp/eps1_backup

Then, I compare the two directory structures

> vimdiff /tmp/eps1 /tmp/eps1_backup

vimdiff gives me a pretty, colorised view - side by side - of my backup and final data, making it easy to tell if anything is missing.

The colours vary from system to system but the gist of it is as follows:

  • The pink rows indicate differences between file a (/tmp/eps1) and file b (/tmp/eps1_backup). The highlighted portion of the pink row - in this instance movies1 - indicate what is different between file a and and file b. The difference here is due to the file list being pulled from different locations.
  • The purple / blue combination indicate something is missing. I have removed AlbumArt* from /tmp/eps1.

This way, when it comes to running that scary command:

rm -rf /media/movies1/eps1

You can be safe in the knowledge you haven’t lost anything.