27

I have a 4GB SD card with some family pictures on it that I need to recover. When I insert the card into my card reader, it shows up as an unknown 32MB device (as /dev/sde) and cannot be mounted. When inserting back into the camera (a Nikon D60), it says the cards needs to be formatted (as does inserting it into a Windows machine). I want to recover all of the pictures on the card (there were others before the family pictures) because I don't know how many I took or their exact sizes (but I believe they were all JPEGs). The card should be formatted as a FAT32 filesystem.

What Linux or Unix utilities are available to recover the files? Can I do it myself or do I need to seek professional help?

Edit: It appears that my card reader has damaged the card in some way, making it unreadable and and unformattable. When I checked another card that was the exact same (save for no files), it "ruined" the second one. I would like to use the second card again, so is there a tool to format a damaged card that doesn't know (or cannot report properly) how large it is?

nopcorn
  • 9,559

5 Answers5

35

First, from your experience with the second card, it seems that your reader is damaged and now damages the cards you insert into it. Stop using that reader immediately, and try to recover the card with another reader. If your data is at all valuable, try to get a brand-name reader with better quality than a bottom-price one.

If the card is merely partly unreadable and not completely unreadable, first try to copy what you can from the card to an image file. Don't use dd for this as it'll stop reading on the first error. Use tools such as dd_rescue or ddrescue. Both tools try to grab as much data as possible from the disk. Example usage (/dev/sdc being the device corresponding to the card; if you don't know which one it is, run cat /proc/partitions and pick the one that seems to have the right size):

ddrescue -dr3 /dev/sdc card.image logfile

Since it looks like the filesystem structure is damaged (your OSes offer to format the drive because they don't see a valid filesystem on it), you'll have to try to recover the files individually. Fortunately, image files start with a recognizable header, and there are many existing carving tools that recognize images: Foremost, MagicRescue, PhotoRec (from the makers of TestDisk), RecoverJPEG, …

Most of these tools are available on typical unix distributions. But if you prefer, you can run a special-purpose distribution or other live CD including recovery tools such as SysRescueCD, Knoppix, CAINE

  • I think most of my troubles stem from the cards being recognized as only 32MB, when they should be 4GB. And I have no way of telling what the low-level cylinders, heads and sectors are. –  Jun 07 '11 at 23:49
  • 2
    @Tyler: If your card isn't recognized at the right size by the hardware, replace the reader immediately. If you tried copying the image and the copy stopped due to errors after 32MB, try again with a tool like ddrescue. You don't need to know anything about cylinders/heads/sectors. – Gilles 'SO- stop being evil' Jun 08 '11 at 00:02
  • What does the option -dr3 do? I am trying to recover from a SD card but that doses not mount. I get the following:Unaligned read error. Is sector size correct? – user3236841 Sep 21 '19 at 18:06
  • 1
    From the man pages the -dr3 option stands for -d, which means "use direct disc access", bypassing any kernel caches of the disc contents, and -r3, which means to retry reading 3 times if it gets an error for a bad sector. – Sean Dec 08 '19 at 10:54
  • The option d leads to Can't open input file: Invalid argument exits, when you try to do ddrescue for a phone (mtp, smth like /run/user/1000/gvfs/mtp:host=Sony_...). – BairDev Nov 05 '21 at 07:52
12

PhotoRec from http://www.cgsecurity.org/ - free, open source, easy to use. Just recovered loads of family photos and videos from a corrupt SD card. Simply unzip into a folder, run the file and point it to the drive.

user36649
  • 121
8

I'd recommend TestDisk. I've used it successfully to recover files from corrupted hdd's. I normally use a distribution like TRK or SystemRescueCd.

agn
  • 156
6

You have not detailed on how data got lost. If it is a logical deletion, and device has not seen much writing, chances of recovering data are good: the obvious advice is to go for TestDisk & Photorec. Of course, the traditional recommendation: write protect the original device, anyway, expose it to the least possible interaction. Best of all, dd it on some different support, unplug it and work on the image file. If, on the other hand, `bad' in the subject means physically ruined, things get tougher, but you should try all the same, because usually a minor portion of the blocks get unreadable. Especially on SD, which have a reputation of very high endurance.

Kevin
  • 40,767
  • I'm not sure how, but the card doesn't seem physically damaged. And I'm not very familiar with dd, so how might I recover the image of the card? What specifically would I need to do with the image? –  Jun 07 '11 at 17:33
  • Try to find a reliable way to connect the sd to a pc. Suppose you manage and the raw device is mapped onto block device /dev/sda (use dmesg to know its name). Then do dd if=/dev/sda of=/mnt/hda1/image.dd bs=1M where /mnt/hda1 is the mountpoint of some other support large enough. Then unplug the sd, do a copy of image.dd and work on the latter so you don't have to replug the SD. See TestDisk step-by-step guide on its homepage. Hopefully from what you say it sounds like you just have to restore partition table/mbr. Good luck. – user495100 Jun 07 '11 at 18:29
  • 1
    @Tyler: Don't use dd, as it stops on the first error. Use something like ddrescue. – Gilles 'SO- stop being evil' Jun 07 '11 at 22:53
3

I used photorec which comes in the package testdisk. It worked fine for me to recover photos from a sandisk microsd that was corrupted by my phone.

Samue
  • 31