In this post, I am going to show you how to use OS X command line to recover corrupted jpg files from an SD card or a USB disk.

Flash storage devices might be cheap. But they are not known for their reliability. Sometimes their file systems get corrupted. There is a jungle of small and paid recovery software across the internet. The problem is, I never trust them, I am never going to buy one them.

I made a research and met with an open source software called SD Recover. I checked the source code and it was surprisingly simple and comprehensive. I wanted to give a try on my father’s corrupted 2 GB SD card. He wanted to recover photos from his latest Balkans tour.

Installation:

Since the software is distributed as a source code form. We need to compile it. I use Mac and OS X and I already have all the required tools with my XBuild setup.

Firstly clone the repository:

``` git clone git://git.code.sf.net/p/sdrecover/git sdrecover-git

cd sdrecover-git ```

Then we need to compile it. It is not recommended installing the software in the documentation so we are just going to use the output of our build.

Compile

xcodebuild

xcodebuild command should compile the required executable files into the project path.

The compiled tools are:

  • jpgrecover - extracts JPEG image files
  • avirecover - extracts AVI files
  • sdrecover - currently just lists information about the SD card
  • sdsearch - searches for file signatures and other data structures

I will use jpgrecover to save my father’s corrupted jpeg pictures.

When you run ./jpgrecover, you should see that it is asking the missing ‘bad_cardimage.img’ to run. It is just a snapshot of your device. In order to get the snapshot, I am going to use the dd command. But the dd command requires an input source and an output target. The input source is going to be the device.

diskutil list

Enter the command above to list the connected storage devices. We will see the path to the device. In my case, it is /dev/disk2. Literally, the output target is going to be ‘bad_cardimage.img’ file.

dd if=/dev/disk2 of=bad_cardimage.img

Wait for a while to copy the entire device into the ‘bad_cardimage.img’ file.

Once it is finished, you have to run:

./jpgrecover

Congratulations. You saved the day.