keepcalculator

06 Numbers and Powers

Recover Data from ext4: Full Guide

Card No. 06·72581 · Updated · Standard formula, shown below

Yes, you can recover data from an ext4 filesystem, and the success rate largely depends on how quickly you stop using the drive and which tool you use. This guide explains the built-in and third-party options, the steps to follow, and the common pitfalls that can permanently destroy your files.

Why ext4 Recovery Is Different

ext4 is the default filesystem for most Linux distributions. Unlike older filesystems, ext4 uses extents, journaling, and delayed allocation. These features make recovery both easier and harder. The journal can sometimes contain recent file data, but delayed allocation means that a file you just copied may not have been written to disk yet when the crash happened. Also, ext4 does not store a filename in the inode; it stores the inode number and the directory entry separately. This means that after a delete, the directory entry is gone, but the inode and its data blocks may still be intact until overwritten.

How ext4 Stores Files (and Deletes Them)

When you delete a file on ext4, the filesystem marks the inode as free and removes the directory entry. The actual data blocks are not erased. They remain on disk until the filesystem reuses them for new data. This is why recovery is possible. The key is to avoid writing anything to the drive after deletion. Even mounting the filesystem can cause journal replay and metadata updates that may overwrite the very blocks you need.

First Steps: Stop Using the Drive

Before you run any recovery tool, you must stop using the affected drive. That means:

  • Unmount the partition immediately: umount /dev/sdXN
  • If the drive is your root partition, boot from a live USB or another Linux system.
  • Do not run fsck unless you have a backup of the filesystem metadata, because fsck can modify the filesystem.
  • Work on a copy of the drive if possible, using dd to create an image.

Built-in Tools: extundelete and debugfs

Linux offers two main command-line tools that work directly with ext4:

  • extundelete – Restores files by inode number or by matching a path. It can recover files that were deleted recently, but it may fail if the inode has been reused.
  • debugfs – A low-level filesystem debugger. You can use debugfs -r -c -w /dev/sdXN to list deleted inodes with lsdel and then dump them with dump. This is more technical but works when extundelete cannot.

Example with extundelete:

sudo extundelete /dev/sdb1 --restore-all

This creates a directory RECOVERED_FILES in your current directory. The tool needs the partition to be unmounted.

Third-Party Tools: TestDisk and PhotoRec

For more robust recovery, especially after formatting or severe corruption, use TestDisk and PhotoRec (both from the same project).

  • TestDisk – Rebuilds the partition table and can recover deleted partitions. It does not recover individual files, but it can make a lost partition visible again.
  • PhotoRec – Carves files based on their signatures (headers/footers). It ignores the filesystem structure and scans raw data blocks. This works even if the filesystem is damaged, but it will not preserve filenames or folder structure.

PhotoRec is often the last resort because it can recover files that extundelete cannot, but you will need to sort through thousands of files with generic names like f123456.jpg.

How to Choose the Right Tool

Your choice depends on the situation:

  1. File deleted recently, no writes since – Use extundelete first. It is fast and preserves names.
  2. Partition formatted or filesystem corrupted – Use TestDisk to restore the partition, then try extundelete. If that fails, use PhotoRec.
  3. Filesystem severely damaged or overwritten – PhotoRec is your only option. It can recover fragments of files.

Common Mistakes That Ruin Recovery

Many people accidentally destroy their chances of recovery. Avoid these:

  • Continuing to use the computer after deletion – even browsing the web writes cache files to disk.
  • Running fsck automatically – it can move data blocks and overwrite deleted inodes.
  • Installing recovery software on the same drive – the installation itself overwrites data.
  • Rebooting into the same system – the journal may replay and overwrite.

Cost and Commercial Options

Open-source tools are free, but they require technical skill. Commercial tools like R-Studio, EaseUS, and Disk Drill offer graphical interfaces and often recover more files automatically. They typically cost between $50 and $100 for a license. If the data is critical, professional data recovery services can cost hundreds to thousands of dollars, but they have clean-room facilities for hardware failures. For software-level recovery, you can often do it yourself with free tools if you act quickly.

Frequently Asked Questions

Can I recover files after formatting an ext4 partition?

Yes, if the format was quick (not a full overwrite). The old data blocks remain until new files overwrite them. Use TestDisk to restore the partition, then extundelete or PhotoRec. The sooner you stop using the drive, the better.

Does ext4 journaling help recovery?

Sometimes. The journal contains metadata and sometimes data for recent writes. If a file was just created and then deleted, the journal might still have a copy. Tools like extundelete do not use the journal, but forensic tools like ext4magic can. However, the journal is finite and gets overwritten.

Why do recovered files have no names?

Because the directory entry (which holds the name) is deleted. PhotoRec and similar tools scan raw data blocks, so they only see file signatures. They assign generic names. To get the original names, you need a tool that reads the filesystem metadata, like extundelete, and that only works if the inode is still intact.

Can I recover data from a corrupted ext4 drive without unmounting?

Technically you can run tools on a mounted filesystem, but it is risky. The system may write to the drive and overwrite the data you want. Always unmount first, or boot from a live USB.

Is it possible to recover a file that was deleted weeks ago?

It depends on how much the drive has been used since. If the blocks were not overwritten, yes. But with normal use, the chance drops significantly after a few days. For best results, stop using the drive immediately.

What is the difference between TestDisk and PhotoRec?

TestDisk repairs partition tables and boot sectors. PhotoRec recovers files by scanning raw data. They are complementary: use TestDisk to fix the partition, then PhotoRec to extract files if the filesystem is still unreadable.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →