Skip to content

Learn

TestDisk and PhotoRec in depth

Two free, open-source tools that every data recovery hobbyist ends up using. TestDisk finds partitions and undeletes files with their names; PhotoRec carves files out of raw data. Both run straight on a DriveArchive image.

What they are

TestDisk and PhotoRec are written by Christophe Grenier and distributed together. They're free and open source (GPL v2 or later) and run on Windows, macOS, Linux, the BSDs and even DOS. Despite the name, PhotoRec isn't just for photos.

  • TestDisk is filesystem-aware. It reads partition tables and filesystem structures, finds partitions that were deleted or lost, can repair FAT and NTFS boot sectors, and can list and copy out deleted files on FAT, exFAT, NTFS and ext2 while their records survive. Files come back with their original names and folders.
  • PhotoRec is a file carver. It ignores the filesystem and looks for the signatures of hundreds of file formats in the raw data. It works after formatting and on filesystems TestDisk can't read, but recovered files get generic names and lose their folders.

The usual order is: TestDisk first (to see the layout and get named files back), then PhotoRec for whatever is left. If you've read exploring images, this page goes further into both.

Installing

  • Windows: download the Windows zip from cgsecurity.org and extract it. There's no installer: run testdisk_win.exe or photorec_win.exe from the folder. The zip also includes qphotorec_win.exe, a simple graphical version of PhotoRec.
  • macOS: brew install testdisk with Homebrew installs both commands, or use the macOS download from cgsecurity.org.
  • Linux: install the testdisk package, which includes PhotoRec, for example sudo apt install testdisk or sudo dnf install testdisk.

Check it works:

testdisk /version
photorec /version

Opening a DriveArchive image

Download a drive from the archive, make a copy of the .img to work on, and open a terminal in the folder that holds it. Then give the image file as the argument:

testdisk drive-copy.img
photorec drive-copy.img

# Windows (Command Prompt, in the folder with the .exe files)
testdisk_win.exe D:\images\drive-copy.img
photorec_win.exe D:\images\drive-copy.img

Because the image is a file you own, you don't need sudo or administrator rights. Running without them is actually safer: if you mistype, the tools can't even open your real disks. Both tools also read EnCase .E01 images.

Never start these tools without an argument "just to look" on a computer with drives you care about: they'll list your physical disks, and TestDisk can write to a partition table if you tell it to.

Getting around the menus

Both tools use the same text interface:

  • Up/Down arrows move between items (disks, partitions, files).
  • Left/Right arrows move between the menu buttons at the bottom, shown in square brackets like [Proceed].
  • Enter chooses; q goes back one screen or quits.
  • The line at the bottom of each screen tells you which other keys do something there. Read it.

TestDisk: finding partitions

  1. Run testdisk drive-copy.img. Choose [Create] to write a log file (testdisk.log) in the current folder. It's a handy record of what was found.
  2. The image is the only "disk" listed. Check its size looks right and choose [Proceed].
  3. Pick the partition table type. TestDisk highlights the one it detected, and that's almost always right: [Intel] for the classic MBR layout used by most old PC drives, [EFI GPT] for newer ones, [Mac] for old Apple partition maps, and [None] for a disk with a filesystem but no partition table (common on USB sticks and floppies).
  4. Choose [Analyse]. TestDisk shows the partitions the current table describes, with filesystem type, start and end.
  5. Choose [Quick Search]. TestDisk scans for partition and filesystem signatures and lists what it finds, including partitions that were deleted from the table.
  6. Highlight a partition and press P to list its files. If you see a sensible folder tree, the partition is real. Press q to go back.
  7. If something seems missing, press Enter and choose [Deeper Search], which checks far more locations and takes longer.

The final screen offers [Write], which saves the found partitions into the partition table. On your working copy that only changes the copy, and it can be an interesting experiment, but you don't need it to recover files: you can list and copy files straight from the search results with P. On a real drive, writing is the step that can make things worse, so get used to not doing it.

TestDisk: undeleting files

  1. From the main menu choose [Advanced]. You'll see the partitions and a row of options: [Type], [Boot], [List], [Undelete] and [Image Creation] (which ones appear depends on the filesystem).
  2. Select a FAT, exFAT, NTFS or ext2 partition and choose [Undelete].
  3. Deleted files are shown in red. Navigate folders with the arrows and Enter.
  4. Mark files with : (colon), or press a to mark everything. Then press C (capital) to copy the marked files, or c to copy just the highlighted one.
  5. TestDisk shows a folder browser for the destination. Move to an empty folder on a different disk from the image and press C to confirm.

[List] works the same way for existing files. On FAT, the first character of a deleted file's name is lost (it's overwritten by a deletion marker), so names may come back as _ETTER.DOC; that's normal.

[Boot] checks FAT and NTFS boot sectors against their backup copies, which is useful on damaged images. [Image Creation] saves a single partition as its own image file (image.dd), handy if you want to run other tools on just that partition.

PhotoRec: your first carve

  1. Make an empty folder for the results on a disk with plenty of free space, for example recovered/. Carving can produce more data than you expect.
  2. Run photorec drive-copy.img and choose [Proceed] on the image.
  3. You'll see the partitions, plus a line for the whole disk (No partition). Choose the whole disk to catch data outside partitions, or a single partition to focus on it. Leave the buttons at the bottom on [Search] for now; options are described below.
  4. Tell PhotoRec the filesystem type: [ext2/ext3] for Linux ext2/3/4 partitions, [Other] for everything else (FAT, NTFS, HFS+ and so on). This helps it work out block sizes.
  5. Choose [Free] to scan only unallocated space (where deleted files live), or [Whole] to scan everything. [Free] avoids recarving files that still exist and is usually what you want on a readable partition; use [Whole] on a formatted or broken one.
  6. Pick the destination: navigate to your empty recovered/ folder and press C.
  7. PhotoRec runs and shows counts per file type. When it's done, quit with [Quit].

You can skip some of the questions by giving the destination on the command line:

photorec /log /d recovered/recup_dir drive-copy.img   # creates recovered/recup_dir.1, .2, ...

/d sets where results go and /log writes photorec.log. PhotoRec also has a /cmd mode for fully scripted runs; see the PhotoRec documentation if you want to automate it.

PhotoRec options

On the partition screen, move to the buttons at the bottom before choosing [Search]:

  • [File Opt] chooses which file types to look for. Space toggles one type, s toggles all of them, and b saves your choice as the default. Turning off types you don't care about makes results much easier to wade through, for example only jpg, doc, pdf and zip.
  • [Options] has a few useful switches. Paranoid checks recovered files more strictly (leave it on). Keep corrupted files keeps partial files that failed the checks, which is sometimes where the interesting fragments are. Expert mode lets you set the block size and offset by hand.

Making sense of the output

PhotoRec writes into numbered folders, recup_dir.1, recup_dir.2 and so on, a few hundred files each. Files are named like f0012345.jpg: the number is the sector where the file started in the scanned area. Some formats carry their own name inside (ZIP archives, Office documents and others), and PhotoRec uses it where it can.

Some commands to get an overview on macOS or Linux:

# how many files of each type?
find recovered -type f | sed 's/.*\.//' | sort | uniq -c | sort -rn

# move everything into one folder per extension
cd recovered
for f in recup_dir.*/*.*; do ext="${f##*.}"; mkdir -p "by-type/$ext"; mv "$f" "by-type/$ext/"; done

# largest files first (often the most interesting)
find recovered -type f -exec ls -lS {} + | head -20

Expect noise: browser cache images, icons, fragments of system files. Expect some broken files too, especially large ones, because PhotoRec can't follow a file that was stored in several fragments.

Want to see a carved file where it sits in the image? Multiply the sector number in the name by the sector size (usually 512) and look at that offset, keeping in mind that if you scanned a single partition, the number counts from the start of that partition:

xxd -s $((12345 * 512)) -l 256 drive-copy.img

These are real people's files. If carving turns up personal documents, ID scans or private photos, don't share them; delete your copies and report the drive.

Strengths and limits

  • Strengths: free with no limits; run almost anywhere; open image files directly; PhotoRec recognises hundreds of formats; TestDisk is excellent at partition problems; well documented and widely used.
  • TestDisk limits: undelete only covers FAT, exFAT, NTFS and ext2; it doesn't rebuild badly damaged directory trees the way DMDE or R-Studio can.
  • PhotoRec limits: no file names or folders; fragmented files come out broken; lots of output to sort through.
  • The text interface takes a few minutes to get used to. After that it's quick.

Tips

  • Keep the TestDisk log: start and end sectors of partitions are useful later, for example as offsets for The Sleuth Kit (fls -o).
  • Run PhotoRec with [Free] first. If that finds little, try [Whole] on the whole disk.
  • Old FAT drives from the 1990s often have little fragmentation, so carving works surprisingly well on them.
  • Never point the destination folder at the image or at a mounted copy of it.
  • Compare with another tool: whatever PhotoRec finds that TestDisk doesn't is a good hint about what was deleted long ago.

When to use something else