Skip to content

Instantly share code, notes, and snippets.

@MortezaJavadian
Last active July 31, 2025 07:54
Show Gist options
  • Select an option

  • Save MortezaJavadian/6277c297a107ea53eed23a88f48f7aa1 to your computer and use it in GitHub Desktop.

Select an option

Save MortezaJavadian/6277c297a107ea53eed23a88f48f7aa1 to your computer and use it in GitHub Desktop.
Compile and install TestDisk and PhotoRec from source on Debian/Ubuntu-based systems and use those to recover file.

Build, Install and Usage TestDisk and PhotoRec from Source

Prerequisites

Install required dependencies:

sudo apt-get update && sudo apt-get install -y \
    build-essential \
    libext2fs-dev \
    libewf-dev \
    libncurses5-dev libncursesw5-dev \
    ntfs-3g-dev \
    libjpeg-dev \
    uuid-dev \
    zlib1g-dev \
    qtbase5-dev \
    qttools5-dev-tools \
    pkg-config \
    dh-autoreconf \
    gettext \
    git

Clone the Repository

git clone https://github.com/cgsecurity/testdisk.git
cd testdisk

Build

mkdir config
autoreconf --install -W all -I config
./configure
make

(Optional: to build a static binary)

make static

Install

sudo make install

Output Files

After a successful build, the following tools will be created:

  • testdisk – partition recovery tool
  • photorec – file recovery tool
  • fidentify – file type identifier
  • qphotorec – graphical interface for PhotoRec

Usage

TestDisk (Interactive Mode)

sudo testdisk           # Start interactive recovery
testdisk /log           # Enable logging
testdisk /list          # List connected disks

PhotoRec

sudo photorec           # Start interactive file recovery
photorec /log           # Enable logging
photorec /d recovery_dir /cmd device  # Command-line recovery

FIdentify

fidentify yourfile.raw  # Detect file types in disk images or raw files

QPhotoRec (GUI)

qphotorec               # Start GUI version of PhotoRec

Example: Recover Files from a USB Drive

1. Identify USB Drive

sudo fdisk -l

Find your USB device (e.g., /dev/sdb).

2. Create and Delete Partition (For Testing)

sudo fdisk /dev/sdX     # Replace sdX with your USB device

Inside fdisk:

  • Press n → create a new partition
  • Press w → write and exit
  • (Optional) Copy a test file to the USB
  • Run fdisk again:
    • Press d → delete the partition
    • Press w → write and exit

3. Recover with TestDisk

sudo testdisk

Follow the interactive steps to scan and restore the deleted partition.

4. Recover with PhotoRec

sudo photorec

Choose your USB device, file system type, and recovery destination directory.

PhotoRec Advanced Settings

Before starting recovery, you can set setting to how PhotoRec revcver your device:

[Options]

Customize scanning behavior:

  • Paranoid: Yes / No
    If enabled, PhotoRec checks for block consistency to reduce false positives (slower but safer).

  • Keep corrupted files: Yes / No
    If enabled, even damaged files will be saved. Otherwise, they are skipped.

  • Expert mode: Yes / No
    Unlocks advanced options like custom block sizes or forced file system detection.

  • Low memory: Yes / No
    Enables a memory-saving mode for systems with limited RAM (may reduce performance).

Use the arrow keys to navigate and press Enter to toggle values.

[File Opt] – File Format Selection

PhotoRec can recover many types of files. This menu lets you choose which ones to include in the scan.

  • [X] = Selected (will be recovered)
  • [ ] = Not selected (will be skipped)

Controls:

  • Space → Toggle selected format
  • s → Deselect all
  • b → Save selection
  • Enter → Next page
  • q → Back to main menu

Creating Custom File Signatures For PhotoRec

You can download the official documentation for TestDisk and PhotoRec here:

On page 45, there’s a section titled "CREATING CUSTOM SIGNATURE FOR PHOTOREC" — but I try it, it not work as expected.

At the end of page 47, there’s a guide for developing main source code:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment