Deleting a file with rm on Linux cannot be undone with a quick Ctrl+Z, but it does not always mean the data is gone. The filesystem removes the reference to the file and marks its space as free, and until something overwrites it, the content may still be on disk. What you do in the first few minutes usually decides whether you get it back.
Key points on recovering a deleted Linux file in 30 seconds
- Stop writing to the affected disk first: any write can overwrite the data you want back.
- Check the Trash, your editor history and any process that still holds the file open before reaching for tools.
- Identify the real device (LVM, LUKS) and work on an image made with dd or ddrescue on a separate physical disk.
- PhotoRec recovers content, ext4magic and extundelete may restore names on ext3/ext4, and TestDisk is mainly for partitions.
Stop, then check the easy places
Rule number one is to stop using the disk. Do not download anything, do not install recovery software on the same partition and do not copy large files around. If possible, remount the filesystem read-only, or shut the machine down and work from a system booted from other media.
Then check the obvious places. If the file was deleted from a graphical file manager such as Nautilus or Dolphin, it is probably in the user Trash:
ls -la ~/.local/share/Trash/files/Code language: JavaScript (javascript)
If it was open in an editor like VS Code, its Local History or Timeline view may hold an earlier version. There is also a lesser-known trick: if a process still has the deleted file open, such as a service writing to a log, the content is still reachable through /proc. Find it with lsof and copy it from the file descriptor:
sudo lsof | grep '(deleted)'
sudo cp /proc/PID/fd/NUM /other/disk/recovered-fileCode language: JavaScript (javascript)
Identify the filesystem and the real device
If none of that works, find out exactly where the file lived before running anything. df -hT shows the filesystem type and device, and lsblk -f shows the full storage layout:
df -hT /home
lsblk -f
This is where extra layers show up. If the partition is an LVM physical volume, the filesystem lives on a logical volume (something like /dev/mapper/vg_data-lv_home, which you can find with sudo lvs), and that is the device to scan. If it is LUKS-encrypted, you must unlock it with its passphrase first; without it, no recovery tool can read the data, which is exactly the point of encryption:
sudo cryptsetup luksOpen /dev/sdb1 recovery_volume
The type of drive matters too. On a spinning hard drive, deleted blocks usually stay until they are reused. On an SSD, the TRIM command tells the drive which blocks are no longer in use, and the drive may erase them internally. Many distributions run fstrim on a schedule, and once TRIM has processed those blocks there is nothing left to recover.
Always work from an image
If the data matters, do not scan the original disk directly. Create an image and work on the copy, so you can try several tools without touching the source. Store the image on a different physical disk, never on another partition of the same drive. For a healthy disk, dd is enough:
sudo dd if=/dev/sdb1 of=/mnt/backup/sdb1.img bs=4M status=progress conv=noerror,syncCode language: JavaScript (javascript)
If the drive throws read errors, GNU ddrescue is the better choice: it copies the healthy areas first and leaves damaged ones for later. Its manual insists on always using a mapfile (called a logfile before version 1.20), because without one an interrupted rescue cannot be resumed, only restarted:
sudo ddrescue -d -r3 /dev/sdb1 /mnt/backup/sdb1.img /mnt/backup/sdb1.map
Which tool to use
PhotoRec is a signature-based file carver: it scans raw storage for known headers such as JPEG, PDF, ZIP or office documents, and works even when the filesystem is damaged. The trade-off is that it usually loses original names and folder structure, producing files like f0000834.txt, although it can rename some of them using embedded metadata. You can point it at the image with photorec sdb1.img.
extundelete and ext4magic target ext3 and ext4. They use the filesystem journal, which may still hold information about recently deleted files, and can bring back the original filename. ext4magic can, for example, recover files deleted within a given time window, and its -r option only restores complete files that have not been overwritten:
sudo extundelete /mnt/backup/sdb1.img --restore-all --output-dir /mnt/external/recovered
sudo ext4magic /mnt/backup/sdb1.img -r -d /mnt/external/recovered
The journal does not keep that information forever, so time matters. If one tool fails, the other may still succeed.
TestDisk, from the same author as PhotoRec, is mainly for recovering lost partitions and repairing boot sectors and partition tables. It can also undelete files on FAT, NTFS, exFAT and ext2 when they were not fragmented and their space has not been reused, but it is rarely the first choice after a plain rm on ext4.
Whatever the tool, save recovered files to a different disk from the one being scanned. Writing them back to the same drive can overwrite exactly what you are trying to save.
The best recovery is not needing one
Recovery is never guaranteed: sometimes everything comes back, sometimes fragments or nameless files, and sometimes nothing. That is why the most effective tool is a backup. Btrfs and ZFS snapshots can undo a deletion in seconds, utilities such as trash-cli send files to the Trash from the command line instead of destroying them, and code belongs in Git. On servers, use automated backups and, above all, tested restores: a backup that has never been restored is not a guarantee.
Frequently asked questions
Can a file deleted with rm be recovered on Linux?
Often, as long as its space has not been overwritten. Stop writing to the disk as soon as possible and work from an image.
What is the difference between PhotoRec and TestDisk?
PhotoRec recovers file contents by scanning for signatures, usually without names. TestDisk focuses on recovering partitions and repairing boot structures.
Can deleted files be recovered from an SSD?
It is harder. If TRIM has already processed the file blocks, the drive will have erased them and there will be nothing to recover.
Why use ddrescue instead of dd?
ddrescue handles read errors better, rescues healthy areas first and, with a mapfile, can resume an interrupted copy.
Sources:
