BLOG POSTS
    MangoHost Blog / PhotoRec – How to Recover Deleted Files in Linux Ubuntu
PhotoRec – How to Recover Deleted Files in Linux Ubuntu

PhotoRec – How to Recover Deleted Files in Linux Ubuntu

PhotoRec is a powerful open-source file recovery tool that can restore deleted files from various storage devices, even when the file system is severely damaged. This cross-platform utility specializes in deep scanning storage media to recover files based on their signatures rather than relying on file system metadata, making it incredibly effective for Ubuntu systems where traditional recovery methods fail. In this guide, you’ll learn how to install and use PhotoRec, understand its technical mechanisms, explore real-world recovery scenarios, and master the troubleshooting techniques that separate novice users from data recovery experts.

How PhotoRec Works Under the Hood

PhotoRec operates on a fundamentally different principle than most file recovery tools. Instead of attempting to reconstruct file system structures, it performs sector-by-sector analysis of storage devices, searching for known file signatures or “magic numbers” that identify specific file types.

The tool maintains an extensive database of over 480 file formats, each with unique binary signatures. For example, JPEG files begin with the hex signature FF D8 FF, while PNG files start with 89 50 4E 47. PhotoRec scans raw disk sectors, identifies these patterns, then attempts to reconstruct complete files by following the file format specifications.

This signature-based approach makes PhotoRec exceptionally resilient against file system corruption, formatting, and even partial overwriting. However, it also means recovered files lose their original names and directory structure, requiring manual organization afterward.

Installation and Basic Setup

PhotoRec comes bundled with TestDisk in Ubuntu repositories. Installation is straightforward:

sudo apt update
sudo apt install testdisk

For the latest version with extended file format support, you can compile from source:

wget https://www.cgsecurity.org/testdisk-7.1.tar.bz2
tar -xjf testdisk-7.1.tar.bz2
cd testdisk-7.1
./configure
make
sudo make install

Before starting recovery operations, create a dedicated directory for recovered files on a different drive than the one being recovered:

mkdir ~/recovered_files
chmod 755 ~/recovered_files

Step-by-Step Recovery Process

Launch PhotoRec with appropriate privileges:

sudo photorec

The text-based interface will display available storage devices. Here’s the complete recovery workflow:

  • Select the target drive using arrow keys and press Enter
  • Choose the partition to recover from (or select “No partition” for whole disk recovery)
  • Select file system type (ext2/ext3/ext4 for Ubuntu systems)
  • Choose “Free” to scan unallocated space only, or “Whole” for complete partition scan
  • Navigate to your recovery destination directory
  • Press ‘C’ to begin the recovery process

During recovery, PhotoRec displays real-time statistics including elapsed time, recovered files count, and estimated completion time. Large drives can take several hours to complete.

Advanced Configuration and File Type Selection

PhotoRec allows granular control over recovery operations through its file type selection menu. Access this by pressing ‘F’ during the file system selection step:

# Enable specific file types only
[X] jpg  - JPEG images
[X] png  - PNG images  
[X] pdf  - PDF documents
[ ] mov  - QuickTime videos (disabled)
[ ] avi  - AVI videos (disabled)

This selective recovery significantly improves performance and reduces false positives when you know exactly what file types you need to recover.

For automated batch recovery, PhotoRec supports command-line operation:

sudo photorec /dev/sdb1 /cmd recover.txt

Where recover.txt contains recovery parameters:

fileopt,everything,disable
fileopt,jpg,enable
fileopt,png,enable
fileopt,pdf,enable
search
quit

Real-World Recovery Scenarios

Different data loss situations require specific PhotoRec approaches. Here are proven strategies for common scenarios:

Scenario Recommended Settings Expected Success Rate Recovery Time
Accidental deletion Free space scan, specific file types 85-95% 30 minutes – 2 hours
Formatted partition Whole partition scan, all file types 70-85% 2-8 hours
Corrupted file system Whole disk scan, paranoid mode 60-75% 4-12 hours
Physical damage Sector-by-sector with ddrescue first 30-60% 12-48 hours

For SSD recovery, enable paranoid mode to handle wear leveling and TRIM command effects:

sudo photorec /dev/sda /paranoid

Performance Optimization and System Resource Management

PhotoRec’s performance heavily depends on storage I/O patterns and system resources. Monitor resource usage during recovery:

iostat -x 5
top -p $(pgrep photorec)

Typical resource consumption for a 1TB drive recovery:

  • CPU usage: 15-25% of single core
  • RAM usage: 50-100MB
  • Disk I/O: 80-120 MB/s sequential read
  • Recovery output: 10-50 MB/s write (depending on file density)

Optimize performance by ensuring the recovery destination is on a separate physical drive and has sufficient free space (recommend 150% of source partition size).

Comparison with Alternative Recovery Tools

Tool Recovery Method File Systems GUI Available Learning Curve
PhotoRec Signature-based All major formats No (CLI only) Moderate
Extundelete Ext3/4 metadata ext3/ext4 only No Low
Foremost File carving Filesystem-agnostic No Low
Scalpel Enhanced carving Filesystem-agnostic No High

PhotoRec excels in cross-platform compatibility and comprehensive file format support, while tools like extundelete offer better filename preservation for specific file systems.

Troubleshooting Common Issues

When PhotoRec fails to start or crashes during recovery, these diagnostic steps usually identify the root cause:

Permission denied errors:

sudo chmod 666 /dev/sdb
# Or run with proper sudo context
sudo -i photorec

Recovery destination issues:

# Check available space
df -h /recovery/destination
# Verify write permissions
touch /recovery/destination/test_file && rm /recovery/destination/test_file

Incomplete file recovery: Often caused by file fragmentation or partial overwriting. Try increasing the search depth:

sudo photorec /dev/sda /depth,paranoid

False positive files: Signature-based recovery sometimes creates invalid files. Validate recovered files using:

file recovered_files/*
# For images specifically
identify recovered_files/*.jpg 2>/dev/null | wc -l

Best Practices and Data Recovery Workflow

Professional data recovery follows specific protocols that maximize success rates while preventing further data loss:

  • Immediately stop using the affected storage device to prevent overwriting
  • Create a disk image using dd or ddrescue before running PhotoRec
  • Always recover to a different physical drive than the source
  • Document recovery parameters and results for reproducibility
  • Verify recovered files before declaring the operation successful

For critical recovery operations, create a forensic image first:

sudo dd if=/dev/sdb of=/mnt/backup/disk_image.dd bs=4M status=progress
sudo photorec /mnt/backup/disk_image.dd

This approach preserves the original evidence while allowing multiple recovery attempts with different parameters.

PhotoRec integrates well with other forensic tools in the Ubuntu ecosystem. Combine it with The Sleuth Kit for comprehensive forensic analysis, or use GNU ddrescue for imaging damaged drives before recovery attempts.

The tool’s signature-based approach makes it invaluable for incident response scenarios where file system metadata is compromised, and its extensive file format database continues expanding with community contributions through the CGSecurity project.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked