How to Recover Deleted Files from Any Drive in Linux
Hardware failure and careless user feeling adventurous with powerful utilities like dd and fdisk can lead to data loss in Linux. Not only that, sometimes spring cleaning a partition or directory can also lead to accidental deletion of some useful files. If that were to happen, there is no reason to despair. With PhotoRec utility, you can easily recover a variety of files, be it documents, images, music, archives, etc.
Developed by CGSecurity and released under the GPL, PhotoRec is distributed as a companion utility to Testdisk, which can be used to recover and restore partitions. You can use either of these tools to recover files, but each has a task that is best suited for it. Testdisk is best suited for recovering lost partitions. Whether it is because of overwriting or deleting a partition, or a partition that has become unreadable for some reason, Testdisk can help you restore the partition, or at the very least, recover data from it. . But if all you are interested in is recovering deleted files from partition, hard drive or even USB drive, you can use PhotoRec. Although originally designed to recover image files only (hence the name), PhotoRec can be used to recover just about any type of file.
Best of all, PhotoRec works by ignoring the underlying file system on the specified partition, disk or USB drive. Instead, it focuses on the unique signatures left by the different types of files to identify them. This is why PhotoRec can work with FAT, NTFS, ext3, ext4 and other types of partitions.
In contrast, Testdisk only supports a limited number of file systems. The biggest downside to PhotoRec – though a tool that can apparently extract deleted files from Digital Ether might have a downside – is that it doesn’t keep the original file names. This means that the recovered files all have an alphanumeric gibberish name. If this is a problem for you, then consider using Testdisk to recover your lost files first.
To install Testdisk, open a terminal window and update the software repositories first before installing testdisk. We are using an Ubuntu machine for our installation.
$ sudo apt install testdisk
If you are using a Fedora or other RPM-based distribution, use dnf instead.
$ sudo dnf install testdisk
Before you start using PhotoRec, it is important to understand how a file system handles deleted files. When you delete a file, it isn’t immediately zapped into oblivion. Instead, the file system simply marks the file as deleted and allocates the space occupied by the file as available for use. This means that until that space is taken up by another file, the original file is still there and can be recovered using specialized data recovery tools.
It is for this reason that you should ideally stop using the system as soon as you realize that you have deleted an important file, as you will minimize the risk of its space being taken up. Although PhotoRec and Testdisk can recover files from deleted and then overwritten partitions, it decreases the chances of recovering all files.
1. Using the file manager, create a directory where you want to store the files recovered by PhotoRec. This directory should not be on the same partition or device from which you are trying to recover files.
2. Open the terminal and launch PhotoRec with sudo powers.
$ sudo photorec
The Home screen which lists all partitions as well as connected drives and devices will be displayed.
3. Using the up and down arrow keys select the drive you want to recover, then use the left and right keys to to select Proceed and press enter. You can speed things up by limiting the recovery to finding specific file types, such as pdf, jpg, or mp3.
After selecting the device or partition from which you want to recover deleted files, use the right / left arrow keys to select the File option and press Enter. If your selected device has multiple partitions, PhotoRec will display all partitions and allow you to choose the partition that housed the deleted file.
4. Select the type of file system used on the drive you want to recover. If the drive had typical Linux partitions, select ext2 / ext3. For anything else, choose the Other option.
5. Using the up and down arrow keys, select whether you want to search and recover files from free space on the drive or from the entire drive.
6. Select the destination of the recovered files. Press Enter to open the directories. When ready press C to set the destination and start the recovery process
Give meaning to recovered files
Depending on the size of the partition or the device, PhotoRec may take some time to complete the full search for files. PhotoRec creates several directories such as recup_dir.1 Where recup_dir.2 in the specified destination directory for the recovered files. You will find all the recovered files in these directories, but PhotoRec does not attempt to sort the different files based on their name or file format.
You can, however, use a few quick CLI-fus to organize the recovered files.
1. Create directories for different file formats, such as mp3 or jpg.
2. Use the mv command to move files in a format specific to the relevant repertoire. For example here we are moving all the jpeg images to the images directory.
$ mv ~/miracles-happen/recup_dir.*/*.jpg ~/miracle-happens/images/
This tutorial first appeared in an issue of Linux format magazine.