Disclosure: some links on this page are affiliate links. If you sign up through them we may earn a commission at no extra cost to you. It helps keep LinuxDistroFinder free.
You ran rm -rf and your stomach dropped. Or maybe you emptied the Trash, formatted the wrong partition, or a disk error ate your files. Whatever happened — stop writing new data to that disk immediately and read this guide. Linux doesn't truly erase file data the moment you delete it; it just removes the directory entry and marks those disk blocks as free. As long as those blocks haven't been overwritten by new data, recovery is very possible.
This guide walks through every realistic recovery scenario on Linux in 2026 — from simple Trash recovery, to deep filesystem scanning with TestDisk and PhotoRec, to ext4-specific tools like extundelete, to raw sector carving with Foremost. We cover ext4, BTRFS, XFS, and NTFS (for externals). Real commands, real expected output, no fluff.
Step 0 — Check the Trash First
If you deleted files via a GUI file manager (Nautilus, Thunar, Dolphin, Nemo), they almost certainly went to the Trash rather than being immediately unlinked. Check ~/.local/share/Trash/files/ before doing anything else. On GNOME you can just open Nautilus, click "Trash" in the sidebar, and restore from there.
# List Trash contents from the terminal
ls -lah ~/.local/share/Trash/files/
# Restore a specific file back to your home directory
mv ~/.local/share/Trash/files/myfile.txt ~/myfile.txtIf the file is there — you're done. If not, or if you used rm directly in the terminal (which bypasses Trash entirely), keep reading.
Understand What Actually Happens When You Delete a File on Linux
On ext4 (the most common Linux filesystem), deleting a file does three things: the directory entry pointing to the inode is removed, the inode's link count drops to zero (freeing the inode), and the block bitmap marks those data blocks as available. The actual bytes on disk are not zeroed — they sit there until something else is written on top.
This is why recovery works — and why speed matters. The moment your OS starts writing other data (logs, browser cache, swap, package installs), it may overwrite those blocks. On SSDs with TRIM enabled, the situation is worse: TRIM can instruct the controller to erase freed blocks almost immediately, making recovery on modern SSDs much harder than on HDDs.
fstrim.timer (runs weekly). If your SSD had TRIM active, recovery odds drop significantly. If you caught the deletion within minutes, you may still have a chance — unmount or power off immediately.Tool Comparison at a Glance
| Tool | Best For | Filesystem Support | Difficulty | Cost |
|---|---|---|---|---|
| TestDisk | Partition recovery, lost boot, undelete on FAT/NTFS | FAT, NTFS, ext2/3/4, exFAT | Intermediate | Free |
| PhotoRec | File carving by signature (photos, docs, video) | Any (raw scan) | Beginner | Free |
| extundelete | Recovering recently deleted files on ext3/ext4 | ext3, ext4 | Intermediate | Free |
| Foremost | Raw sector carving, any filesystem | Any (raw) | Intermediate | Free |
| Scalpel | Faster Foremost alternative, configurable headers | Any (raw) | Intermediate | Free |
| Btrfs restore | Snapshot/subvolume recovery on BTRFS | BTRFS only | Intermediate | Free |
| xfs_repair | XFS filesystem repair (not undelete) | XFS only | Advanced | Free |
Method 1 — TestDisk (Best All-Rounder)
TestDisk is the Swiss Army knife of Linux recovery. It can recover lost partitions, fix boot sectors, and — crucially for us — undelete files on FAT and ext2/ext3 filesystems. On ext4 it can still browse the filesystem and copy files that are still partially intact. Install it from your package manager:
# Debian / Ubuntu / Linux Mint
sudo apt install testdisk
# Fedora / RHEL
sudo dnf install testdisk
# Arch Linux
sudo pacman -S testdiskTo recover deleted files, launch TestDisk against your disk (not a mounted partition). Identify the correct device first:
# List block devices to find the right disk
lsblk
# Launch TestDisk (run as root)
sudo testdisk /dev/sdaIn the interactive menu: choose No Log → select your disk → choose your partition table type (usually Intel/MBR or EFI/GPT) → select Advanced → choose the partition → select Undelete. TestDisk will display files marked as deleted (shown in red). Navigate to a file, press C to copy it to a destination you choose. Always copy to a different disk.
extents enabled (the default since Ubuntu 9.10), it may not list all deleted files — use extundelete instead for ext4.Method 2 — PhotoRec (File Carving, Works on Any Filesystem)
PhotoRec — despite the name — recovers far more than photos. It recognizes over 480 file formats including PDF, DOCX, ZIP, MP4, SQLite databases, and more. It works by scanning raw disk sectors for known file headers (magic bytes), bypassing the filesystem entirely. This means it works even if the partition table is damaged, the filesystem is corrupted, or the drive was reformatted.
# PhotoRec ships with testdisk
sudo apt install testdisk # includes photorec
# Launch against the disk or a partition
sudo photorec /dev/sdb
# Or use a disk image (safer — always image first if possible)
sudo ddrescue /dev/sdb /mnt/backup/disk.img /mnt/backup/disk.map
sudo photorec /mnt/backup/disk.imgIn the PhotoRec menu: select your disk → choose the partition → choose the filesystem type (leave as Other if unsure) → choose Free (scan only free space) or Whole (scan everything) → navigate to a different disk as your output directory → press C to start. PhotoRec will create numbered folders (recup_dir.1, recup_dir.2, etc.) full of recovered files.
Caveat: PhotoRec does not recover original filenames. You'll get files named f0001234.jpg, f0002345.pdf, etc. For photos this is usually fine; for documents you may need to open each one to find what you want.
Method 3 — extundelete (ext3/ext4 Specialist)
extundelete reads the ext3/ext4 journal to find recently deleted inodes and attempts to reconstruct the file. It's your best bet for recovering a specific named file from an ext4 partition within minutes to hours of deletion.
# Install extundelete
sudo apt install extundelete
# Unmount the partition first — NEVER run recovery on a mounted RW partition
sudo umount /dev/sda2
# Recover all deleted files from the partition
sudo extundelete /dev/sda2 --restore-all
# Recover a specific file by name (faster)
sudo extundelete /dev/sda2 --restore-file path/to/deleted/myfile.txt
# Recover an entire directory
sudo extundelete /dev/sda2 --restore-directory /home/user/Documentsextundelete creates an RECOVERED_FILES/ directory in your current working directory. Run these commands from a different mounted disk, e.g., cd /mnt/external_drive before running extundelete, so recovered files land there.
Method 4 — Foremost and Scalpel (Deep Raw Carving)
When everything else fails, Foremost and Scalpel do byte-level carving. They're slower than PhotoRec but their configuration files let you target specific file types precisely.
# Install both
sudo apt install foremost scalpel
# Run Foremost — recover JPEGs and PDFs from /dev/sdb into /mnt/recovery
sudo foremost -t jpg,pdf -i /dev/sdb -o /mnt/recovery/foremost_out
# Scalpel — edit config first to enable desired file types
sudo nano /etc/scalpel/scalpel.conf
# (uncomment lines for the file types you want, e.g., jpg, png, pdf)
sudo scalpel /dev/sdb -o /mnt/recovery/scalpel_outScalpel is typically 2–3× faster than Foremost on large drives because it uses multi-threading and makes only one pass. Both tools output an audit.txt report showing what was found and where.
Method 5 — BTRFS Snapshots (If You Were Using BTRFS)
If your system uses BTRFS — which is the default on openSUSE, Fedora 33+, and can be set up on Ubuntu — you may have automatic snapshots via Snapper. This is the easiest recovery method of all when available.
# List available snapshots
sudo snapper list
# Browse the files in snapshot #42
ls /.snapshots/42/snapshot/home/user/Documents/
# Copy the deleted file out of the snapshot
cp /.snapshots/42/snapshot/home/user/Documents/myfile.txt ~/myfile.txt
# Alternative: use btrfs restore to read a damaged subvolume
sudo btrfs restore /dev/sdb1 /mnt/recoveryCreating a Disk Image Before Recovery (Always Recommended)
Before running any recovery tool on a failing or accidentally wiped drive, image it first. ddrescue is the best tool for this — unlike dd, it handles read errors gracefully and logs which sectors were successfully read so you can retry failed ones.
# Install ddrescue (GNU ddrescue, not dd_rescue)
sudo apt install gddrescue
# Image /dev/sdb to an image file on /dev/sdc (external drive)
# -d = direct disc access, -r3 = retry bad sectors 3 times
sudo ddrescue -d -r3 /dev/sdb /mnt/external/disk.img /mnt/external/disk.map
# Now run recovery tools on the image, not the original drive
sudo photorec /mnt/external/disk.img
sudo extundelete /mnt/external/disk.img --restore-allThis protects the original drive from further wear and gives you a safe copy to experiment on repeatedly.
Tool Rankings by Scenario
Works on any filesystem, any damage level. Recovers 480+ file types by signature. Completely free, ships with TestDisk. The single best tool to try first in most scenarios. Downsides: no filename recovery, outputs thousands of files in bulk.
Reads the ext4 journal to pinpoint recently deleted inodes. Recovers original filenames and directory structure. Must run within a short window before the journal is overwritten. Unmount the partition first.
The goto tool for lost partitions, corrupted MBR/GPT, and undeleting files on FAT/NTFS. Its file browser lets you manually copy intact files even from ext4. Interactive TUI makes it beginner-friendly once you know the menu flow.
Faster than Foremost, highly configurable via /etc/scalpel/scalpel.conf. Ideal when PhotoRec misses unusual or proprietary file types. Requires editing the config to enable file types before first run.
If you're on Fedora, openSUSE, or a BTRFS setup with Snapper configured, recovering a deleted file is as simple as copying from a snapshot directory. Zero complexity, 100% success rate for covered files. Set this up now before you need it.
🖥️ Want reliable Linux cloud backups so you never need recovery tools?
Prevention beats recovery every time. Spin up a Linux VPS and run automated rsync, Restic, or BorgBackup jobs nightly to an off-site location. Vultr gives you $100 free credit to get started — enough for months of backup storage.
Claim $100 Free Credit on Vultr →Prefer a managed option? Hostinger VPS starts at ₹149/mo and includes NVMe storage — ideal for a dedicated backup server.
Prevention: Stop Needing Recovery Tools
The best file recovery is the one you never have to do. Here are concrete habits that will save you:
- Use
trash-cliinstead ofrm. Install it (sudo apt install trash-cli) and aliasrmtotrash-putin your~/.bashrc. Files go to Trash instead of being immediately unlinked. - Set up BTRFS with Snapper or Timeshift. Automatic hourly snapshots save you from every accidental deletion. On ext4, Timeshift can do rsync-based snapshots to an external drive.
- Run automated offsite backups. Restic + a VPS or Backblaze B2 bucket gives you versioned, encrypted backups. A single
restic backup ~/Documentscron job is enough. - Enable SSD TRIM carefully. TRIM is good for SSD health but makes recovery impossible. If you work with sensitive or irreplaceable files, consider keeping a periodic backup rather than relying on recovery after the fact.
- Use
rm -ias an alias. The-iflag prompts for confirmation before every deletion. Annoying, but a lifesaver.
# Add these to ~/.bashrc to make rm safer
alias rm='rm -i'
alias del='trash-put'
# Install trash-cli
sudo apt install trash-cli
# View and restore from CLI trash
trash-list
trash-restoreFrequently Asked Questions
Can I recover files deleted with rm on Linux?
Yes, often — especially on HDDs and if you act quickly. The rm command unlinks the file from the directory but does not zero the disk blocks. Tools like extundelete (ext4), TestDisk, or PhotoRec can recover these files provided the blocks haven't been overwritten by new data yet.
Does recovery work on SSDs?
It's harder on SSDs. If TRIM is enabled and the OS has sent TRIM commands to the controller (usually within minutes to hours of deletion), the SSD may have already erased the freed blocks at the hardware level, making recovery impossible. On SSDs without TRIM (or where TRIM hasn't run yet), standard tools still work. Unmount and power off the drive immediately for the best chance.
Do I need to unmount the partition before running recovery tools?
Yes — always unmount before running extundelete, TestDisk undelete, or any write-capable recovery operation on the target partition. A live mounted filesystem will keep writing (logs, journaling, atime updates) and actively overwrite your recoverable data. Boot from a Live USB if you need to recover from your root partition.
Which tool is best for recovering photos from a formatted SD card?
PhotoRec is ideal for this. It bypasses the filesystem entirely and scans raw sectors for JPEG, PNG, RAW, and video file headers. Run it against the SD card device (e.g., /dev/sdc) and output to a separate drive. It will recover thousands of files even from a quick-formatted card.
Can I recover files from a reformatted partition?
Yes, often. A quick format only rewrites the filesystem metadata (superblock, inode tables, directory structure) — it does not overwrite the actual data blocks. PhotoRec and Scalpel can still find and carve files from the old data area. A full format (which writes zeros) makes recovery impossible.
What if my hard drive is making clicking noises?
Stop using it immediately. Clicking is a sign of physical head failure. First, try ddrescue to image as much of the drive as possible before it fails completely. If ddrescue fails, you'll need professional data recovery services (e.g., Ontrack, DriveSavers) — do not open the drive yourself. Software tools cannot help with physical damage.
PhotoRec recovered my files but without filenames. How do I sort them?
For photos, use ExifTool to read EXIF metadata and rename files by date/camera model. For documents, a simple script using file and grep can sort by type. For office documents, tools like catdoc or LibreOffice can batch-open and preview content to help identify files.
Is there a GUI option for file recovery on Linux?
Yes — TestDisk has a graphical frontend called QPhotoRec (Qt-based). Install it with sudo apt install qphotorec. It provides a point-and-click interface for the PhotoRec engine. For a fully graphical experience, the Gnome Disks app can help with partition issues, though not file-level recovery.