points failure and careless users using powerful utilities (such as dd and fdisk) may cause data loss in Linux. Not only that, sometimes cleaning up partitions or directories in spring can also cause accidental deletion of some useful files. If this happens, there is no reason to despair. Using the PhotoRec utility, you can easily recover all kinds of files, including documents, images, music, archives, etc.
PhotoRec is developed by CGSecurity and released under the GPL. It is distributed as a supporting utility for Testdisk and can be used to restore and restore partitions. You can use any of these tools to recover files, but each tool has the most suitable job. Testdisk is best for recovering lost partitions. Whether this is because you overwrite or delete the partition, or the partition becomes unreadable for any reason, Testdisk can help you recover the partition, or at least, recover data from it. However, if you only want to recover deleted files from partitions, hard drives, or even USB drives, you can use PhotoRec. Although originally designed to recover only image files (hence the name), PhotoRec can be used to recover files in almost any way.
Even better, 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 different file types to identify them. This is why PhotoRec can handle FAT, NTFS, ext3, ext4 and other partition types.
In contrast, Testdisk only supports a limited number of file systems. The biggest drawback of PhotoRec-if any tool that appears to be able to extract deleted files from digital ether has a drawback-it does not retain the original file name. This means that the recovered files have garbled alphanumeric names. If this is a problem for you, please consider using Testdisk to recover lost files first.
To install Testdisk, open a terminal window and update the software repository before installing testdisk. We use an Ubuntu machine for installation.
$ sudo apt install testdisk
If you are using Fedora or other RPM-based distributions, please use dnf instead.
$ sudo dnf install testdisk
Before you start using PhotoRec, it is important to understand how the file system handles deleted files. When you delete a file, it will not be forgotten immediately. 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 before the space is occupied by another file, the original file still exists and can be retrieved using a special data recovery tool.
It is for this reason that you’d better stop using the system as soon as you realize that you have deleted an important file, because this can minimize the risk of its space being occupied. Although both PhotoRec and Testdisk can recover files from deleted and then overwritten partitions, it reduces the chance of recovering all files.
1. Using the file manager, create a directory The location where you want to store the files recovered by PhotoRec. This directory should not be located on the same partition or device from which you are trying to recover files.
2. Open a terminal and start PhotoRec with sudo permissions.
$ sudo photorec
You will see a welcome screen listing all partitions and connected disks and devices.
3. Use the up and down arrow keys Select the drive you want to recover, Then use the left and right keys choose continue with Press Enter. You can help speed it up by restricting the recovery to search for 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 File options Button and press Enter. If the device you select contains multiple partitions, PhotoRec will display all the partitions and allow you to select the partition where the deleted files are located.
4. Select the type of file system used on the drive to be recovered. If the drive has a typical Linux partition, choose ext2/ext3. For anything else, please choose another option.
5. Use the up and down arrow keys, Choose whether to search and restore files from the free space of the drive or from the entire drive.
6. Select the destination of the restored files. Press Enter to open the catalog.When ready Press C to set destination And start the recovery process
Understand the recovered files
Depending on the size of the partition or device, PhotoRec may take a long time to perform a complete file search. PhotoRec has created several directories, such as recup_dir.1 or recup_dir.2 Restore files in the specified target directory. You will find all recovered files in these directories, but PhotoRec will not try to sort different files based on name or file format.
However, you can use some quick CLI-fu 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 Related catalogs in a specific format. For example, here we move all jpeg images to the image directory.
$ mv ~/miracles-happen/recup_dir.*/*.jpg ~/miracle-happens/images/
This tutorial first appeared on Linux format Magazine.