How to Recover Deleted Files on Linux
Table of Contents
Accidentally deleting important files is a common issue that many users face. Fortunately, on Linux systems, it is often possible to recover deleted files using a variety of tools and techniques. This guide will walk you through the process of recovering deleted files on Linux using TestDisk, Extundelete, and PhotoRec.
Using TestDisk
TestDisk is a powerful, open-source data recovery tool designed to help users recover lost partitions and make non-booting disks bootable again.
TestDisk is primarily used for:
- Recovering deleted or lost partitions: TestDisk can scan and recover partitions that have been accidentally deleted or damaged due to software issues, viruses, or human error.
- Repairing partition tables: TestDisk can fix partition table errors, enabling users to access and recover their data.
- Fixing boot sectors: TestDisk can rebuild boot sectors and repair errors that prevent a system from booting up.
- Recovering deleted files: TestDisk can also recover deleted files from FAT, exFAT, NTFS, ext2, ext3, and ext4 file systems.
- Copying files from deleted or damaged partitions: TestDisk allows users to copy files from partitions that are deleted or damaged to another healthy partition or storage medium.
TestDisk is a command-line utility, which might seem intimidating for users unfamiliar with the command line. However, the tool offers a text-based interface that guides users through the recovery process, making it relatively easy to use for people with varying levels of technical expertise.
Step 1: Stop Writing to Affected Partition
When a file is deleted, the space it occupied is marked as free, but the data is not immediately overwritten. To maximize the chances of successful recovery, stop using the affected partition as soon as you realize that you need to recover a deleted file.
Step 2: Open Terminal
Open the terminal by pressing Ctrl + Alt + T on your keyboard or by searching for it in the applications menu.
Step 3: Install TestDisk
On Debian/Ubuntu-based distros, run:
sudo apt install testdisk
On Fedora/RPM-based distros, run:
sudo dnf install testdisk
On Arch-based distros, run:
sudo pacman -S testdisk
Step 4: Recover Deleted Files
Run TestDisk with root privileges:
sudo testdisk
Select No Log and press Enter. Select the appropriate disk and press Enter. Select the partition table type (usually “Intel” or “EFI GPT”) and press Enter. Select Advanced and press Enter. Select the affected partition and press Enter. Select Undelete and press Enter. Browse the list of deleted files and select the ones you want to recover. Press C to copy the selected files to a safe location. Exit TestDisk once the recovery process is complete.
Using Extundelete
Extundelete is an open-source utility designed specifically for recovering deleted files from ext3 and ext4 file systems, which are commonly used in Linux distributions. It works by reading the file system’s journal to find and restore the deleted files. Extundelete is a command-line tool, and although it doesn’t have a graphical user interface, it is relatively easy to use.
Extundelete can be particularly useful for recovering files that were accidentally deleted using the rm command or deleted from the file manager without being moved to the trash first. However, it’s essential to act quickly after a file is deleted, as the space occupied by the deleted file could be overwritten by new data, making recovery impossible.
Some of the main features of Extundelete include:
- Recovering individual files or entire directories
- Restoring files to their original location or a specified directory
- Recovering files based on their inode number
- Displaying information about deleted files without actually recovering them
It’s important to note that the success of file recovery with Extundelete depends on various factors, such as the amount of time elapsed since the file was deleted and whether the file system has been extensively used after the deletion. To improve the chances of successful recovery, it’s recommended to stop using the affected partition and perform the recovery process as soon as possible.
Step 1: Stop Writing to Affected Partition
When a file is deleted, the space it occupied is marked as free, but the data is not immediately overwritten. To maximize the chances of successful recovery, stop using the affected partition as soon as you realize that you need to recover a deleted file.
Step 2: Open Terminal
Open the terminal by pressing Ctrl + Alt + T on your keyboard or by searching for it in the applications menu.
Step 3: Install Extundelete
On Debian/Ubuntu-based distros, run:
sudo apt install extundelete
On Fedora/RPM-based distros, run:
sudo dnf install extundelete
On Arch-based distros, run:
sudo pacman -S extundelete
Step 4: Recover Deleted Files
Unmount the affected partition by running:
sudo umount /dev/sdXY
Replace sdXY with the appropriate device and partition identifiers.
Run Extundelete with the following command:
sudo extundelete /dev/sdXY –restore-file /path/to/deleted/file
Replace sdXY with the appropriate device and partition identifiers and /path/to/deleted/file with the path of the deleted file. If successful, Extundelete will recover the deleted file and save it in a RECOVERED_FILES directory.
Using PhotoRec
PhotoRec is an open-source data recovery software designed to recover lost files from various types of storage media, such as hard drives, memory cards, and USB flash drives. It supports a wide range of file systems, including FAT, exFAT, NTFS, ext2, ext3, ext4, and HFS+. Despite its name, which suggests a focus on photo recovery, PhotoRec can actually recover a variety of file types, including images, documents, videos, and archives.
Developed by Christophe Grenier, the creator of TestDisk, PhotoRec is a command-line utility that features a text-based interface, making it relatively easy to use even for those who are not familiar with the command line.
The key features of PhotoRec include:
- Recovery of lost files from various file systems and storage media
- Support for a large number of file formats
- Recovery of files even if the file system is severely damaged or reformatted
- Optional expert mode for advanced settings and fine-grained control over the recovery process
PhotoRec works by searching for file signatures (i.e., patterns in the file headers) to identify and recover lost files. It does not rely on the file system’s structure, which allows it to recover files even when the file system has been damaged or reformatted. However, since it does not take the file system’s metadata into account, recovered files might have generic names and not retain their original directory structure.
Step 1: Stop Writing to Affected Partition
When a file is deleted, the space it occupied is marked as free, but the data is not immediately overwritten. To maximize the chances of successful recovery, stop using the affected partition as soon as you realize that you need to recover a deleted file.
Step 2: Open Terminal
Open the terminal by pressing Ctrl + Alt + T on your keyboard or by searching for it in the applications menu.
Step 3: Install PhotoRec
PhotoRec is bundled with TestDisk, so when you install TestDisk, you will also install PhotoRec. To install TestDisk (and consequently, PhotoRec):
On Debian/Ubuntu-based distros, run:
sudo apt install testdisk
On Fedora/RPM-based distros, run:
sudo dnf install testdisk
On Arch-based distros, run:
sudo pacman -S testdisk
Step 4: Recover Deleted Files
Run PhotoRec with root privileges:
sudo photorec
Select the appropriate disk and press Enter. Select the partition table type (usually “Intel” or “EFI GPT”) and press Enter. Select the affected partition and press Enter. Select the file system type and press Enter. Select a directory to save the recovered files and press C. PhotoRec will start scanning the partition and recovering deleted files. Once the process is complete, browse the recovery directory for your recovered files.
Conclusion
By following this guide, you should be able to recover deleted files on your Linux system using TestDisk, Extundelete, and PhotoRec. Each tool offers unique capabilities and can be used in different scenarios to maximize your chances of successful recovery. However, it is important to remember that file recovery is not always guaranteed, so it is essential to maintain regular backups of your critical data. Developing a habit of creating backups and practicing good file management can help minimize the risk of data loss and ensure that your valuable information remains safe and accessible.