Recursively MD5 multiple files in OS X

I have an archive of very large files that I would like to preserve into the future. I wanted to produce an MD5 hash of all the files so that as the years pass, I can be certain that the files are exactly the same as they started (i.e. not corrupted by aging storage media). As a Mac user, I knew about the md5 command, but unfortunately this command only works on one file at a time and will not recursively hash a directory full of files. I also knew about the md5deep project which specifically adds this functionality, but alas there wasn't an OS X binary available anywhere that I could find. Upon Googling for such a program, there were a bunch of nice GUI-based programs for Windows, but nothing for OS X.

This morning when I went into work I explained my problem to Brad who suggested that I investigate if the find command would be of any use. Indeed, this was great advice and probably explains why there isn't a ready app in OS X to do it - essentially this functionality is built in!
find * -iname '*.dv' -exec md5 '{}' \; >output.txt
The above command will find all files that match "*.dv" (i.e. any file with an extension of dv) in the current directory and below, and then produce an MD5 hash of that file. It will then output the list of all the md5s to a file called output.txt.

So, basically if you are ever looking for a program where you can drop a folder into it to check and include subfolders to do a particular activity (and you're not afraid to use the command line), you can adapt the technique above by changing the md5 command to any other command.

Epilogue: I originally wrote this post when I was using OSX Leopard, but it also works in Snow Leopard, Lion and Mountain Lion and indeed should work with any flavour of OSX.

2 comments:

  1. Excellent. Was having issues trying to force feed md5 from ls and grep. Works in Lion 10.7 (still) as well.

    ReplyDelete
  2. Thank you! I was trying to find a way to generate a hash file for all my backup files, as I do with md5summer. This helped me a lot!

    ReplyDelete