How to Recover Deleted Files in Ubuntu via TestDisk

s2 0

We all know the feeling of losing a file, looking for it in the Recycle Bin and not even finding it there. Fortunately, the trauma is temporary and there are several tools available to help you recover your lost file. Most of these tools restore the lost files from the previous images (memory status) of your system. One of these tools is the proven and highly reliable TestDisk utility. TestDisk is free data recovery software designed to restore lost partitions and / or make non-booting hard drives bootable again if these symptoms are caused by faulty software, certain types of viruses, or human error. It can also be used to repair some file system errors.

In this article, we will explain how to recover your accidentally lost files on Ubuntu using the TestDisk utility.

We ran the commands and procedures mentioned in this article on an Ubuntu 20.04 LTS and Ubuntu 18.04 LTS system. These instructions also work under Debian 10.

The scenario

So the situation is that I accidentally deleted a document file (.docx) that was in my / home /, even from the trash.[user]/ Downloads folder. Now I need to find a way to restore the file to its previous location. Or at least restore it to somewhere I can access it and then move it to my required directory.

Here is a step-by-step process to help me and you recover an accidentally deleted / lost file in Ubuntu.

Step 1: Install the TestDisk utility

Open your Ubuntu command line, Terminal, either by searching in the system application launcher or by using the keyboard shortcut Ctrl + Alt + T.

Then run the following command as sudo to install the TestDisk utility:

$ sudo apt-get install testdisk

Please note that only an authorized user can add / remove and configure software on Ubuntu. Please enter the password for sudo, then the utility will be installed on your system.

You can check that the utility is actually installed on your system and also check its version number with the following command:

$ testdisk --version


$ testdisk -v

Step 2: Run testdisk and create a new testdisk.log file

Use the following command to run the testdisk command line utility:advertising

$ testdisk

The output contains a description of the utility. You can also create a testdisk.log file. This file will later contain useful information on how and where your lost file was found, listed, and resumed.

Start the test disk utility

The above output gives you three options of what to do with this file:

Create: (Recommended) – This option allows you to create a new log file.

Append: This option allows you to append new information to information already listed in this file from a previous session.

No Minutes: Select this option if you do not want to record anything about the session for later use.

Important: TestDisk is a pretty smart tool. It knows that many beginners will use the utility to recover lost files as well. Hence, it predicts and suggests the option that you should ideally choose on a given screen. You can see the suggested options in a highlighted form. You can use the up and down arrow keys to select an option, and then press Enter to make your choice.

In the output above, I would choose to create a new log file. The system may ask you for the password for sudo at this point.

Step 3: Select your recovery drive

The utility will now display a list of the storage drives attached to your system. In my case, it shows my hard drive as it is the only storage device on my system.

Choose a recovery drive

Use the left / right arrow keys to select Continue and press Enter. As mentioned in the note in the screenshot above, the correct hard drive capacity must be detected in order for a successful file recovery to take place.

Please note that your system may not show all connected devices at this point due to security permissions. If your desired device isn’t listed, you’ll be given one more option besides Continue and Exit: the Sudo option. Select the Sudo option, enter the Sudo password and the system will now display all of your system’s drives.

Step 4: Choose the partition table type of your selected drive

Now that you’ve selected a drive, you’ll need to specify its partition table type on the following screen:

Select partition table type

The utility automatically highlights the correct selection. Press Enter to continue.

If you are sure that the test disk intelligence is incorrect, you can make the correct selection from the list and then press Enter.

Step 5: Choose the “Advanced” option for file recovery

When you have specified the correct drive and its partition type, you will see the following screen:

Advanced file recovery options

Recovering lost files is just one of the features of testdisk; There’s a lot more to the utility. You can select one of these features using the options shown in the screenshot above. But here we are only interested in recovering our accidentally deleted file. To do this, select the Advanced option and press Enter.

If you get to a point in this utility that you did not intend, press the q key to go back.

Step 6: Select the drive partition where you lost the file

If your selected drive has multiple partitions, you can select the appropriate one in the following screen.

Select the partition that contains the lost file

I lost my file while using Linux, Ubuntu. Make your choice, then select the list option from the options that appear at the bottom of the screen.

This will list all of the directories on your partition.

Step 7: Navigate to the directory from which you lost the file

When the testdisk utility shows all of your operating system’s directories, navigate to the directory from which you deleted / lost the file. I remember losing the file from the Downloads folder in my home directory. So I flip home:

Select the directory that contains the file to be restored

Then my username (sana):

Home directory

And then the download folder:

Downloads folder

Tip: You can use the left arrow to return to the previous directory.

When you have reached your desired directory, you will see the deleted files in colored or highlighted form.

And here you can see my lost file “accidental_remote.docx” in the list. Of course it says so because I had to show you the whole process.

Lost file is indicated by testdisk

Step 8: Copy the deleted file to be recovered

By now, you must have found your lost file on the list too. Use option C to copy the selected file. This file will later be restored to the location you specify in the next step.

Step 9: Provide the location where the found file will be restored

After copying the lost file that we have now found, the testdisk utility will display the following screen for us to indicate where to restore it.

You can specify any accessible location as it is just a simple interface to copy and paste the file into the location you want.

I specifically choose the location where I lost the file, my Downloads folder:

Select the location for the restore

Step 10: Copy / Restore the file to the selected location

After making the selection where you want to restore the file, click the c button. This will restore your file to this location:

Recover lost file

Do you see the green text in the screenshot above? This is really great news. Now my file will be restored to the specified location.

This may seem a bit of a tedious process, but it is definitely worth getting your lost file back. The recovered file is most likely in a locked state. This means that only an authorized user can access and open it.

We all need this tool over and over again, but if you want to delete it until you need it further, you can do so with the following command:

$ sudo apt-get remove testdisk

You can also delete the testdisk.log file if you want. Have fun recovering your lost file and thank you TekDisk!

How to Recover Deleted Files in Ubuntu via TestDisk