Third party cookies may be stored when visiting this site. Please see the cookie information.

Home Family Days Out Raspberry Pi & Electronics Blog

Stewart's Blog

Stewart Watkiss website to the world ...

(Children, computing, first aid and other ramblings ...)

TestDisk – Free unformat / SD card recovery tools for Linux, Windows and Mac

Ever had that sinking feeling when you format your SD card with all your precious holiday photos before suddenly realising that you hadn’t actually copied them anywhere? In this case it turned out I did have a copy after all, it just happened to be on a different computer. I thought it was a good opportunity to practice restoring files, something I’ve not really had to deal with since I lost half my 5,000 word essay stored on a floppy disk [remember them?] when I was at college. The disk drive was faulty and damaged both my regular and backup floppy disks.

I found TestDisk & PhotoRec, a disk / file recovery tool available free under the GPL from CG Security. TestDisk download.

The following instructions are for use with Linux. These are based on recovery of photos from a Nikon D50 digital SLR. Recovery is done on an EeePC netbook with hard disk drive and built-in SD card reader running Eeebuntu (Ubuntu based Linux distribution). It should be similar whatever operating system is being used.

The program is available as a pre-built binary for each operating system. For Linux it is available as either a tar.bz2 file or as an RPM. There is a single package suitable for either i386 or AMD 64bit processors. To install from the tar download to the local computer and then

bunzip2 testdisk*.tar.bz2
tar -xvf testdisk*.tar
cd testdisk*
cd linux
sudo ./photorec_static

Here the SD card is quite obvious. It is labelled as a Flash Reader and is 2GB compared with the hard disk of 160GB.

PhotoRec 6.11.3, Data Recovery Utility, May 2009
Christophe GRENIER

PhotoRec is free software, and

Select a media (use Arrow keys, then press Enter):
Disk /dev/sda - 160 GB / 149 GiB (RO) - ATA ST9160310AS
Disk /dev/sdb - 2032 MB / 1938 MiB (RO) - Single Flash Reader

[Proceed ] [ Quit ]

Disk capacity must be correctly detected for a successful recovery.
If a disk listed above has incorrect size, check HD jumper settings, BIOS
detection, and install the latest OS patches and disk drivers.

The partition type should be Intel/PC for a digital camera which will have the FAT filesystem.

PhotoRec 6.11.3, Data Recovery Utility, May 2009
Christophe GRENIER

Disk /dev/sdb - 2032 MB / 1938 MiB (RO) - Single Flash Reader

Please select the partition table type, press Enter when done.
[Intel ] Intel/PC partition
[EFI GPT] EFI GPT partition map (Mac i386, some x86_64...)
[Mac ] Apple partition map
[None ] Non partitioned media
[Sun ] Sun Solaris partition
[XBox ] XBox partition
[Return ] Return to disk selection

Note: Do NOT select 'None' for media with only a single partition. It's very
rare for a drive to be 'Non-partitioned'.

Choose the appropriate partition to search for any files.

PhotoRec 6.11.3, Data Recovery Utility, May 2009
Christophe GRENIER

Disk /dev/sdb - 2032 MB / 1938 MiB (RO) - Single Flash Reader

Partition Start End Size in sectors
No partition 0 0 1 984 40 40 3970048 [Whole disk]
1 P FAT16 >32M 0 3 61 983 63 63 3967239 [NIKON D50]

[ Search ] [Options ] [File Opt] [ Quit ]
Start file recovery

FAT should then be selected rather than the ext2 / ext3 filesystems (which is used for most Linux filesystems). If you are trying to recover from a Linux hard disk then you should be choosing ext2 / ext3.

PhotoRec 6.11.3, Data Recovery Utility, May 2009
Christophe GRENIER

1 P FAT16 >32M 0 3 61 983 63 63 3967239 [NIKON D50]

To recover lost files, PhotoRec need to know the filesystem type where the
file were stored:
[ ext2/ext3 ] ext2/ext3/ext4 filesystem
[ Other ] FAT/NTFS/HFS+/ReiserFS/...

You will then need to select a location to store the files. This needs to be on a different disk drive to the one you are recovering from to prevent risking overwriting any files that have not yet been recovered.

Then leave the program running, which may take some time. In my case just over 10 minutes for a 2GB SD card. The files will have lost their original filenames, so may need to be renamed.

Assuming the files haven't already been overridden then they should be found and saved in the selected folder.

Good luck!

Please leave comments using the facebook comments below.

UK days out, children and holiday information is also available on the Days Out Diary web site
Linux, LPI and the Quiz / Test Program posts are also available on the Penguin Tutor website
First Aid Information, first aid games and first aid practice test / quiz entries are also available on the First Aid Quiz Web site