Picture Perfect Rescue After Human Error

A few days ago I was called for help from a family member because instead of deleting one picture on the camera, a friend accidentally deleted ALL pictures on the SD card. Obviously there weren’t just a few images on the SD card, no, it was close to a thousand. Probably happening a million times a day on the planet but I have to admit that was a bit of a challenge to fix remotely.

I don’t do rescue operations like this very often but I knew two things: First, there are tools to restore accidentally deleted images for Linux. And secondly, never work on the original, immediately make a block level backup of that SD card with ‘dd’ so things can’t get any worse and only work with the backup. And just to be really sure, I made a copy of the 16 GB image file so I could try different things without having to make another backup of the original SD card first. So while the PC was copying the 16 GB SD card to an image file on the disk of the PC, I was looking into software options for image recovery.

I pretty quickly stumbled over PhotoRec for Linux, a command line tool that works on image files created by ‘dd’ (or similar tools). Use is super easy and within a few minutes it restored the 985 images that were accidentally deleted. The joy of the owner was obvious but I soon figured out that not all was quite as it should have been. All images were restored but they had new filenames with seemingly random names. The file dates were restored correctly so one could still sort by date but sorting the jpg images by filename no longer worked. When you have 985 unsorted images, that’s a problem.

But then I remembered that jpg files have date and time they were taken in their EXIF information. So there must be a way to use that to get the date and time back into the filename. Another search request later and I found a nifty bash command line instruction sequence that does just that:

for i in *.jpg; do exiv2 -v -r '%Y%m%d.%H%M%S.restored_pics' rename "$i"; done

And voilà, a few seconds later, the name of all files was set to the date and time they were taken and could be sorted again by name to get the correct order. Search engines can really be helpful at times.

So after a tense hour or two, all images that were accidentally deleted were restored and I donated 25 euros to the author of PhotoRec, because that software really saved the day! And probably the next one, too, because I am sure that despite the experience, no backups will be made in the future, either.