Restore Removed File Linux: 5 Ways to Recover
In this article
Yes, you can restore a removed file in Linux, but the method depends on how it was deleted and how quickly you act. This article covers the main recovery options, from checking your trash to using specialized tools, and explains what to do immediately after deletion to maximize your chances of success.
How File Deletion Works in Linux
When you delete a file in Linux, it is not immediately erased from the disk. The filesystem simply marks the space as available for reuse. The actual data remains until overwritten by new writes. This is why recovery is possible, but also why you should stop using the drive as soon as you realize a file is missing.
There are two common deletion methods: moving to trash (graphical environments) and permanent deletion (using rm or shift+delete). Trash is a special folder that holds files until you empty it. Permanent deletion bypasses trash entirely, making recovery more difficult but still possible with the right tools.
Check the Trash First
If you deleted a file using a file manager like Nautilus, Dolphin, or Thunar, it likely went to the trash. Restoring it is the simplest and safest option.
- Open your file manager and navigate to the Trash or Wastebasket location (often in the sidebar).
- Locate the file, right-click it, and select Restore or Put Back.
- The file returns to its original location.
If you cannot find it in the trash, it may have been deleted with rm or the trash was emptied. In that case, proceed to the next sections.
Use TestDisk for Partition Recovery
TestDisk is a powerful open-source tool that can recover deleted files by scanning the raw disk. It works best for files that were recently deleted and not yet overwritten.
- Install TestDisk using your package manager:
sudo apt install testdisk(Debian/Ubuntu) orsudo dnf install testdisk(Fedora). - Run
sudo testdiskin a terminal. - Select the disk that contained the file, then choose the partition type (usually Intel for MBR or EFI GPT).
- Select Advanced and then Undelete.
- Navigate to the directory where the file was, select it, and copy it to a different drive.
TestDisk is especially useful for recovering files from ext4, NTFS, and FAT filesystems. It does not require a graphical interface and works from a terminal.
Use PhotoRec for File Carving
PhotoRec, a companion tool to TestDisk, recovers files by scanning for known file signatures (headers and footers) rather than filesystem metadata. It works even if the filesystem is damaged or the partition is lost.
- Install PhotoRec:
sudo apt install testdisk(it is part of the same package). - Run
sudo photorec. - Select the disk and partition to scan.
- Choose the file types to recover (or leave all selected).
- Specify a destination directory on a different drive (never the same one you are recovering from).
- Start the scan; it may take a long time for large drives.
PhotoRec is best for documents, images, and videos. Recovered files are renamed automatically, so you may need to sort through them afterward.
Use extundelete for ext3/ext4
If your filesystem is ext3 or ext4 (common on many Linux distributions), extundelete is a dedicated tool that can restore deleted files using the filesystem journal.
- Install extundelete:
sudo apt install extundelete. - Unmount the partition if possible:
sudo umount /dev/sda1(replace with your partition). - Run:
sudo extundelete /dev/sda1 --restore-file path/to/fileor--restore-allto restore everything. - Files are restored to a
RECOVERED_FILESdirectory in your current location.
Note: extundelete works best if you act quickly, as the journal may overwrite old data. It is less reliable on busy systems.
Choose the Right Tool for Your Situation
Selecting the right recovery method depends on several factors:
- Trash available? Use the file manager restore option.
- Filesystem type? extundelete for ext3/ext4, TestDisk for many others.
- File type? PhotoRec for common formats like JPEG, PDF, ZIP.
- Time since deletion? The sooner you act, the better the chances.
- Drive activity? If you have written new data, recovery may be impossible.
For most users, starting with TestDisk is a good balance of simplicity and effectiveness. PhotoRec is a fallback if TestDisk fails.
Common Problems and Misconceptions
One common misconception is that rm permanently destroys data. In reality, it only removes the directory entry; the data remains until overwritten. However, modern SSDs with TRIM may immediately erase blocks, making recovery impossible. If you have an SSD, act fast and consider using a live USB to avoid further writes.
Another issue is that recovery tools often need root privileges, and running them on a mounted filesystem can cause further damage. Always unmount the partition if possible, or boot from a live USB and work from there.
Also, do not install recovery tools on the same drive you are trying to recover from, as that could overwrite the very data you want to save.
Prevention and Backup Strategies
The best way to "restore" a removed file is to never lose it in the first place. Regular backups are the most reliable safety net.
- Use
rsyncto sync important directories to an external drive or cloud storage. - Set up automated snapshots with tools like
timeshiftorsnapper. - Consider using a version control system like Git for text files and code.
- For critical data, use a redundant storage solution like RAID or a NAS.
If you do not have backups, recovery tools are your only option, and their success is never guaranteed.
Frequently Asked Questions
Can I recover a file deleted with rm?
Yes, often you can, using tools like TestDisk or extundelete, provided the disk space has not been overwritten. Stop using the drive immediately and run recovery from a live USB or after unmounting the partition.
How long does recovery take?
It depends on the tool and the size of the drive. A quick scan with extundelete may take minutes, while a full PhotoRec scan of a large drive can take hours. Patience is key.
Will recovery work on an SSD?
It is less likely due to TRIM, which immediately erases deleted blocks. If you have an SSD, your best chance is to use a tool immediately after deletion, before TRIM runs.
Can I recover files from a formatted partition?
Yes, PhotoRec can often recover files from a formatted partition by scanning for file signatures. However, the filesystem metadata is lost, so you will get a list of files without original names or folder structure.
Is it safe to install recovery tools on the same drive?
No, installing software writes new data, which can overwrite deleted files. Always install tools on a different drive or use a live USB environment.