Recovering deleted files on Linux: ext4, Btrfs and PhotoRec

Updated November 2026

On Linux, rm does not use a trash can, and on ext3 and ext4 it erases the pointers to the file's data straight away. Recovery is still often possible, but only if you stop writing to that file system now. This page gives the order to try things in: the quick wins first (open files, trash, snapshots), then a disk image, then tools that read the ext4 journal or scan raw data.

The first five minutes

  1. Stop writing. Every new file, log line or browser cache entry may land on the blocks that still hold your data. Close programs that write to that disk.
  2. Unmount the file system, or make it read-only. For a data disk: sudo umount /home or sudo mount -o remount,ro /home. If the file was on the root file system, shut down and boot from a live USB instead. Do not install recovery tools onto the affected disk.
  3. Note what you lost: full path, rough size, file type, and when it was deleted. The journal tools below can filter by time.
  4. Pick a different disk for output. Recovered files must never be written to the file system you are recovering from.

SSDs and TRIM. On SSDs and many virtual disks, the system tells the drive which blocks are free (TRIM). Once that happens, the drive may return zeros for those blocks and nothing can bring the data back. Many distributions run fstrim.timer on a schedule (check with systemctl status fstrim.timer), and file systems mounted with the discard option trim immediately. Act fast on SSDs, and see the section on backups at the end.

Quick wins: check these first

Is a program still holding the file open?

A deleted file stays on disk while any process has it open, for example a log file still used by a service, or a video in a player. List open files that have been unlinked:

sudo lsof +L1

If your file is listed, copy it back through /proc, using the process ID (PID) and file descriptor number (FD, without the letter) from the output:

sudo cp /proc/1234/fd/7 /root/recovered-file

Trash, editors and copies

Snapshots

If the file system takes snapshots, the old version is probably still there, read-only, and you can just copy it.

And of course: restore it from your backup, if you have one. Backups are the only reliable answer.

Make an image before experimenting

Recovery tools read the disk, but mistakes (or a failing disk) can make things worse. If the data matters, copy the whole partition to another drive first and work on the copy. For a healthy disk, dd works. For a disk with read errors, use GNU ddrescue, which skips bad areas and can resume using its mapfile:

lsblk -f                                       # find the partition, e.g. /dev/sdb2
sudo apt install gddrescue                     # Fedora: sudo dnf install ddrescue
sudo ddrescue -d /dev/sdb2 /mnt/backup/sdb2.img /mnt/backup/sdb2.map   # -d: direct disc access

The image needs as much free space as the partition is large: a 500 GB partition needs a 500 GB file on the other drive. All the tools below accept an image file in place of a device name.

ext3 and ext4: use the journal

When ext3 or ext4 deletes a file, it clears the block list in the file's inode, so the old trick of reading the inode no longer works. But the file system journal often still holds an older copy of that inode. Two tools use this. Both need the file system unmounted (or an image), and both take paths relative to the file system's own root: a file at /home/alice/report.odt on a /home partition is alice/report.odt.

extundelete

sudo apt install extundelete          # Debian/Ubuntu; Debian 13 ships 0.2.4
cd /mnt/backup                        # output goes to ./RECOVERED_FILES here
sudo extundelete /dev/sdb2 --restore-file alice/report.odt
sudo extundelete /dev/sdb2 --restore-directory alice/projects
sudo extundelete /dev/sdb2 --restore-all --after $(date -d "2 hours ago" +%s)

Files it cannot name come back as file.NNNN. Use file on them to see what they are.

ext4magic

ext4magic reads the same journal information but can often restore more, including whole directory trees with their original names, owners and times.

sudo apt install ext4magic
# one file deleted within the last day, written under /mnt/backup/recover
sudo ext4magic /dev/sdb2 -r -f alice/report.odt -a $(date -d "-1 day" +%s) -d /mnt/backup/recover
# everything that was deleted
sudo ext4magic /dev/sdb2 -m -d /mnt/backup/recover

Never point ext4magic at the journal of a file system that is still mounted read-write: it reads half-written blocks and gives wrong results. Unmount first, or use an image.

Both tools are old, but still packaged in Debian 13 (ext4magic 0.3.2, extundelete 0.2.4). They work best minutes after a deletion. After hours of normal use, the journal has usually been overwritten.

Old ext2 file systems

ext2 has no journal and does not clear the block lists, so debugfs from e2fsprogs can still list deleted inodes with lsdel and copy them out. That is what the classic version of this HOWTO described. ext2 is rare today; TestDisk's "Undelete" menu also handles it.

Any file system: PhotoRec

When the journal approach fails, or the file system is XFS, Btrfs, FAT, exFAT or NTFS, use PhotoRec. It ignores the file system and scans raw blocks for known file headers: ZIP, office documents, PDF, HTML, JPEG and more than 480 file extensions in all. It opens the source read-only.

sudo apt install testdisk             # Fedora, or RHEL with EPEL: sudo dnf install testdisk
sudo photorec /mnt/backup/sdb2.img    # or a device such as /dev/sdb2

Choose the partition, the file system family, whether to carve only the unallocated space (enough for deleted files) or the whole partition, and an output directory on another disk. Results land in recup_dir.1, recup_dir.2 and so on. The catch: file names and folders are lost, and fragmented files may be incomplete. Sort the results by type and size, or search them with grep -rl 'a phrase you remember' recup_dir.*.

The same package's TestDisk recovers lost or deleted partitions and rewrites partition tables. Use it if a whole partition disappeared, not for single files. As of November 2026 the current TestDisk and PhotoRec release is 7.2.

Btrfs and XFS

Btrfs is copy-on-write, so old copies of data can survive for a while, but there is no supported undelete tool. btrfs restore exists to pull files out of a damaged Btrfs file system without changing it; it is not meant for single deleted files. For XFS there is no undelete in xfsprogs. On both, snapshots or backups are the realistic answer, and PhotoRec is the fallback.

What to expect

Make sure it never happens again

The classic 1999 Ext2fs Undeletion mini-HOWTO, which recovered ext2 files by editing inodes with debugfs, is kept by the LDP at tldp.org.

Sources