Recover Deleted Files Ubuntu Linux: 7 Methods
In this article
- How File Deletion Works on Ubuntu
- Stop Using the Drive Immediately
- Method 1: Check the Trash and Backups First
- Method 2: extundelete for ext3/ext4
- Method 3: TestDisk for Partition and File Recovery
- Method 4: PhotoRec for Raw File Carving
- Choosing the Right Tool
- Common Problems and Misconceptions
- Frequently Asked Questions
If you deleted a file on Ubuntu and it is no longer in the Trash, you can still recover it using command-line tools like extundelete, testdisk, or photorec, provided you stop using the drive immediately. The sooner you act, the higher the chance of getting your data back. This guide covers the main recovery methods, how they work, and how to choose the right one for your situation.
How File Deletion Works on Ubuntu
When you delete a file in Ubuntu, the operating system does not immediately erase the data from the disk. Instead, it removes the file's entry from the filesystem index (the inode table on ext4, for example) and marks the space as available for overwriting. The actual bytes of the file remain on the storage medium until new data is written over them. This is why recovery is possible, but it is also why using the drive after deletion can permanently destroy the file. Every write operation, even a small log update, can overwrite the sectors that hold your deleted file.
Ubuntu's default filesystem is ext4, but you may also be using ext3, XFS, Btrfs, or NTFS on a dual-boot system. The recovery method depends heavily on the filesystem, because each one stores metadata differently. For example, ext3 and ext4 have a journal that can help, while XFS is more complex and often requires specialized tools.
Stop Using the Drive Immediately
The single most important step in any data recovery is to stop writing to the affected drive. If the file was on your main system drive, shut down the computer and boot from a live USB stick. If the file was on a secondary data drive, unmount it right away and do not open any applications that might write to it. Even mounting the drive read-write can update access times, so use the mount -o ro option if you need to access it.
For a live USB, you can create one using Ubuntu's built-in Startup Disk Creator or a tool like Rufus on Windows. Boot from the USB, select “Try Ubuntu,” and then install the recovery tools onto the live session. This way, the hard drive remains untouched while you work.
Method 1: Check the Trash and Backups First
Before reaching for advanced tools, verify that the file is not in the Trash. In the file manager, click “Trash” in the sidebar. If you deleted it with rm in the terminal, it never went to the Trash, but it is worth checking anyway. Also, check if you have any backup systems enabled: Ubuntu's built-in Deja Dup (Backups) or Timeshift snapshots can restore older versions of files. If you use cloud storage like Nextcloud or Google Drive, check their trash folders, which often retain deleted files for 30 days.
If you have a system backup, restoring from it is almost always easier and safer than trying to recover raw data. Consider this step as a free and quick option before spending time on low-level recovery.
Method 2: extundelete for ext3/ext4
extundelete is a command-line utility that works on ext3 and ext4 filesystems. It attempts to restore files by reading the filesystem journal and inode data. To install it, run:
sudo apt install extundelete
Then, to recover files from a partition like /dev/sda1 into a directory called recovered:
sudo extundelete /dev/sda1 --restore-all --output-dir recovered
This command scans the partition and tries to restore every deleted file. You can also specify a single file with --restore-file path/to/file. The tool works best if the filesystem was unmounted cleanly and the files were deleted recently. It may fail if the inode has been reallocated or if the journal has been overwritten.
Method 3: TestDisk for Partition and File Recovery
TestDisk is a powerful tool that can recover lost partitions and repair boot sectors, but it also has a file recovery mode. It works on many filesystems, including ext4, NTFS, FAT, and HFS+. Install it with:
sudo apt install testdisk
Run it with sudo testdisk and follow the interactive menu. Choose the disk, select the partition type, and then choose “Advanced” > “Undelete” to see deleted files. TestDisk is particularly useful when the partition itself is damaged or when you need to recover files from a filesystem that extundelete does not support.
Method 4: PhotoRec for Raw File Carving
PhotoRec is a companion tool to TestDisk that recovers files by scanning the raw disk for known file signatures. It does not care about the filesystem structure, so it works even if the filesystem is corrupt or formatted. It can recover documents, images, videos, and archives. Install it with sudo apt install testdisk (it comes with the same package). Run it with sudo photorec and select the disk and partition. Choose where to save the recovered files (on a different drive, ideally). PhotoRec will produce many files with generic names like f1234567.jpg, so you will need to sort through them after recovery.
This method is the most thorough but also the most time-consuming. It can recover files that other tools miss, but it will not preserve original filenames or folder structures.
Choosing the Right Tool
The best tool depends on your situation:
- extundelete: Best for ext4 systems where the file was deleted recently and the filesystem is healthy. Fast and preserves filenames.
- TestDisk: Best for recovering entire partitions or when the filesystem is damaged. Also works on NTFS and other non-ext filesystems.
- PhotoRec: Best when the filesystem is heavily corrupted or formatted, or when you need to recover specific file types. Slow but thorough.
- GUI alternatives: Tools like R-Linux or Disk Drill (free for limited recovery) provide a graphical interface, but the command-line tools are just as effective and free.
If you are not sure which to use, start with TestDisk's undelete option, as it is the most balanced. If that fails, try PhotoRec.
Common Problems and Misconceptions
A common misconception is that rm permanently deletes a file. In reality, it only unlinks the file; the data remains until overwritten. Another misconception is that you can recover files from an SSD as easily as from a hard drive. SSDs use wear leveling and TRIM, which immediately erases deleted data blocks, making recovery nearly impossible. If you have an SSD, your chances are slim unless you have a backup.
Another issue is that recovery tools must be run with root privileges (sudo), and they may not work if the filesystem is mounted. Always unmount the partition before attempting recovery. Also, do not install the recovery tool onto the same drive you are trying to recover from, as that installation process can overwrite the very data you need.
Frequently Asked Questions
Can I recover a file deleted from the command line with rm?
Yes, as long as the data has not been overwritten. The same tools (extundelete, TestDisk, PhotoRec) work for files deleted with rm because the deletion process is the same at the filesystem level. The file is not in the Trash, but the bytes are still on the disk.
How long does a deleted file remain recoverable?
There is no fixed time. It depends on how much new data is written to the drive. On an active system drive, a file might be overwritten within minutes. On a rarely used data drive, it could last for months. The only safe assumption is that the file is recoverable until overwritten, so act quickly.
Can I recover files from an SSD?
Very unlikely if the drive supports TRIM, which is enabled by default in Ubuntu for most SSDs. TRIM tells the drive to erase unused blocks immediately, so deleted files are gone almost instantly. If you have an SSD, your best bet is a backup or a snapshot taken before deletion.
What if the file was on a Btrfs or XFS filesystem?
extundelete does not support these filesystems. For Btrfs, you can try btrfs restore or btrfs-find-root. For XFS, the xfs_repair tool has a recovery option, but it is complex. In many cases, PhotoRec is the only viable option because it scans raw data regardless of the filesystem.
Is it safe to install recovery tools on the same drive?
No. Installing software writes new data to the drive, which can overwrite the deleted file. Always use a live USB or a different drive for the recovery tools. This is the most common mistake that ruins recovery attempts.
Can I recover a file that was deleted months ago?
It depends on the drive activity. If the drive has been used regularly, the file is likely overwritten. If the drive has been sitting idle, it might still be there. There is no way to know without scanning, so it is worth trying, but do not expect success after a long time.