KhueApps
Home/DevOps/4 Ways to Recover Accidentally Deleted Files in Ubuntu

4 Ways to Recover Accidentally Deleted Files in Ubuntu

Last updated: February 07, 2025

Accidentally deleting important files can be a stressful experience, especially on a robust system like Ubuntu. Fortunately, when files are deleted, they are not immediately removed from the hard drive; instead, the space they occupied is simply marked as available for new data. With prompt action and the right tools, you can often recover your lost files. This article explores four methods for recovering deleted files on Ubuntu.

Preliminary Considerations

Before starting the recovery process, keep these guidelines in mind:

  • Stop using the affected partition immediately to prevent new data from overwriting your deleted files.
  • Establish a regular backup routine using tools such as rsync or Deja Dup to minimize potential data loss.
  • Identify your file system type (e.g., ext3 or ext4) so you can choose the most appropriate recovery tool.

Method 1: Using the TestDisk Tool

TestDisk is a powerful open-source tool designed for recovering lost partitions and undeleting files. It supports a wide range of file systems and is effective for both partition recovery and file restoration.

Installation

Step 1: Update your package list and install TestDisk:

sudo apt update
sudo apt install testdisk

File Recovery Process with TestDisk

Step 1: Launch TestDisk:

sudo testdisk

Step 2: Follow the on-screen prompts:

  • Choose to create a new log file.
  • Select the disk that contains your deleted files.
  • Choose the appropriate partition table type (usually detected automatically).

Step 3: Navigate to the Advanced menu for file recovery.

Step 4: Select the partition containing your lost files and choose the Undelete option.

Step 5: Browse through the list of recoverable files, mark the ones you wish to restore, and follow the prompts to copy them to a safe location (preferably on another drive).

Method 2: Using the PhotoRec Tool

PhotoRec, bundled with TestDisk, specializes in recovering a variety of file types, including videos, documents, and archives. It scans the disk at a low level, making it effective on hard disks, CD-ROMs, and memory cards.

Recovery with PhotoRec

Step 1: Launch PhotoRec:

sudo photorec

Step 2: Select the disk from which you want to recover files.

Step 3: Choose the partition containing the deleted files.

Step 4: Specify the file system type (e.g., ext4, FAT32) when prompted.

Step 5: Choose a destination directory to save the recovered files (make sure it is on a different drive to avoid overwriting).

Step 6: Allow PhotoRec to scan the drive; the recovered files will be saved in the designated directory.

Method 3: Recovering from the Trash

If you accidentally delete files using Ubuntu’s graphical environment (such as the Nautilus file manager), the files may be moved to the Trash rather than permanently deleted.

Step 1: Open the Trash from your desktop or file manager.

Step 2: Browse for the files you need to recover.

Step 3: Right-click on the desired file and select Restore to return it to its original location.

Method 4: Using Extundelete for ext3/ext4 File Systems

For systems using ext3 or ext4 file systems, extundelete is a useful tool that leverages the disk’s metadata to recover deleted files.

Installation

Step 1: Update your package list and install extundelete:

sudo apt update
sudo apt install extundelete

Recovery Process with Extundelete

Step 1: Identify the partition where the files were deleted using the lsblk command:

lsblk

Step 2: Run extundelete, replacing /dev/sdxn with the appropriate partition identifier (for example, /dev/sda1):

sudo extundelete /dev/sdxn --restore-all

Step 3: The recovered files will be saved in a directory named RECOVERED_FILES in your current working directory.

Final Thoughts and Best Practices

Timeliness is crucial when it comes to data recovery. The sooner you act, the higher the chances of success. Avoid writing new data to the affected partition to prevent overwriting the deleted files. Regular backups and familiarization with recovery tools like TestDisk, PhotoRec, and extundelete can be lifesavers in data loss situations.

Data loss can happen to anyone, but with the proper knowledge and tools, you can recover your valuable files on Ubuntu. Whether you prefer command-line tools or a graphical interface, the methods described above offer several strategies to safely restore your lost data.

Happy recovering!

Next Article: Making GET requests with cURL: A practical guide (with examples)

Previous Article: How to check if a port is currently in use on Ubuntu

Series: Ubuntu

DevOps