How to Recover Deleted Files on Linux – Step by Step

It may be necessary at some point Recover deleted files on certain devices such as a USB key or a microSD memory card. Linux has one, among many others, which is very efficient and fast: test disc.

How to Recover Deleted Files in Linux

To do this job, we need to use the Terminal. It is not complicated if we follow these steps carefully.

One of the first steps to take is to know exactly the unit, or partition, from which we are going Recover deleted fileswe can use a Terminal command:

df –Th

view connected devices

In the previous image, we have a view of the result that the command returns by terminal. We need to take note of the unit to use this data in the following steps.

Let’s go on. We will now install a program that we need to do the task of Recover deleted files using the terminal.

The program in question is “Testdisk”. This has the function of recovering one or more files from a disk, a USB key or a USB memory. We install it this way in Debian family distributions:

sudo apt install testdisk

If you are using Fedora, you can install it like this:

sudo dnf install testdisk

The following is similar in any Linux distribution where we have Testdisk installed.

We need to start Testdisk with root permissions, and for this we write the command:

sudo testdisk

testdisk create activity log

The image shows that the first step is to create an activity log. By default, the “Create” option is already selected. We press “Enter” to continue.

In the next window, it will ask us to choose the device or partition from which we want to recover files. As you can see in the following image, in the example we are going to use a USB stick that has the /dev/sdb drive assigned to it.

choose device

We get on unity, and make sure the “Continue” option is selected below. We press “Enter”. And then the type of format that TestDisk detected for our USB flash drive will appear in the window. We press “Enter” again to continue.

In the next window we will choose the “Advanced” option, this is the one indicated to recover files. We move the selection with the “up” “down” arrows of the cursor and press “Enter”

choose the action to perform

In the new window, it will show the partition type, which for the example with the USB drive is FAT32. We check that “Undelete” is selected in the lower menu, and press “Enter”. TestDisk will start scanning the drive and show us the file(s) that were deleted and can be recovered. As we see in the image below, the files are highlighted in red.

test disk: deleted files
testdisk: deleted files to recover

To select the files that we want to recover, we must go to the file and then press the key: (colon). Each time we press it, the file changes color from red to green and goes to the next line.

On the bottom bar of TestDisk are instructions for choosing files. The options are:

  • Right arrow to choose directory
  • h to hide deleted files
  • what to go out
  • : (colon) to select the file on which we are positioned
  • a to select all files in the list
  • C (uppercase) to copy all selected files
  • c (lowercase) to copy only the file on which we are positioned

When we have all the files selected from which we want to recover, we will press the “C” key (note that it must be the capital C for all files to be copied).

In the next window it will ask us to choose the directory where we want to save the files that can be recovered. In the example, we will save the files in “Documents” of the hard drive and press “Enter”. As a good practice, it is advisable not to try to save the files on the same medium from which we try to save them. Recover deleted files.

save files

In the last window, just press the uppercase C key for the files to be copied. TestDisk brings us back to the screen to select files in case we want to continue recovering files.

If we go to the “Documents” folder, we will see that all files have been successfully recovered.

The first time, it may take us a little time to do the recovery, but with practice, using TestDisk will be very intuitive and efficient for us. Recover deleted files.

If you have any questions or want to ask us about any of the steps we saw, write us in the comments so we can help you.