Recover Deleted Files Using TestDisk tool in Ubuntu
TestDisk is a free data recovery software designed to help recover lost partitions and/or make non-booting disks bootable again when these symptoms are caused by faulty software, certain types of viruses, or human error. It can also be used to repair some filesystem errors.
Step wise commands and procedures mentioned in this article on an Ubuntu 20.04 LTS and Ubuntu 18.04 LTS system. This guide also works on Debian 10.
Step 1: Install the TestDisk utility
Open the terminal command line in Ubuntu.
Then run the following command as sudo in order to install the TestDisk utility:
$ sudo apt-get install testdisk
For this only an authorized user can add/remove and configure software on Ubuntu. You have to enter the password for sudo after which the utility will be installed on your system.
Step 2: Check its version number
You can verify if the utility is indeed installed on your system and also check its version number by using the following command:
$ testdisk –version
Step 3: Run testdisk and create a new testdisk.log file
Use the following command in order to run the testdisk command-line utility:
$ testdisk
The output will give you a description of the utility. It will also let you create a testdisk.log file. This file will later include useful information about how and where your lost file was found, listed, and resumed.
The above output gives you three options about what to do with this file:
Create: (recommended)- This option lets you create a new log file.
TestDisk is a pretty intelligent tool. It does know that many beginners will also be using the utility for recovering lost files. Therefore, it predicts and suggests the option you should be ideally selecting on a particular screen. You can see the suggested options in a highlighted form. You can select an option through the up and down arrow keys and then hit Enter to make your choice.
In the above output, I would opt for creating a new log file. The system might ask you the password for sudo at this point.
Create: (recommended)- This option lets you create a new log file.
Append: This option lets you append new information to already listed information in this file from any previous session.
No Log: Choose this option if you do not want to record anything about the session for later use.
In the above output, I would opt for creating a new log file. The system might ask you the password for sudo at this point.
Select Proceed, through the right/left arrow keys and hit Enter. As mentioned in the note in the above screenshot, the correct disk capacity must be detected in order for successful file recovery to be performed.
Please note that at this point, your system might not display all the connected devices due to security permissions. If your required device is not listed, you will get another option along with Proceed and Quit: the Sudo option. Select the Sudo option, enter the password for sudo and the system will now show all the drives on your system.
Step 4: Select Partition Table Type of your Selected Drive
Now that you have selected a drive, you need to specify its partition table type on the following screen:
The utility will automatically highlight the correct choice. Press Enter to continue.
If you are sure that the testdisk intelligence is incorrect, you can make the correct choice from the list and then hit Enter.
Step 5: Select the ‘Advanced’ option for file recovery
When you have specified the correct drive and its partition type, the following screen will appear:
Recovering lost files is only one of the features of testdisk; the utility offers much more than that. Through the options displayed in the above screenshot, you can select any of those features. But here we are interested only in recovering our accidentally deleted file. For this, select the Advanced option and hit Enter.
In this utility, if you reach a point you did not intend to, you can go back by using the q key.
Step 6: Select the drive partition where you lost the file
If your selected drive has multiple partitions, the following screen lets you choose the relevant one from them
I lost my file while I was using Linux, Ubuntu. Make your choice and then choose the List option from the options shown at the bottom of the screen.
This will list all the directories on your partition.
Step 7: Browse to the directory from where you lost the file
Recover deleted files When the testdisk utility displays all the directories of your operating system, browse to the directory from where you deleted/lost the file. I remember that I lost the file from the Downloads folder in my home directory. So I will browse to home:
Then, my username (linux-ranaji):
And then the Downloads folder:
Tip: You can use the left arrow to go back to the previous directory.
When you have reached your required directory, you will see the deleted files in colored or highlighted form.
And, here is see my lost file “accidently_removed.docx” in the list. Of course, it is named this as I had to illustrate the whole process to you.
Step 8: Copy the deleted file to be restored
Recover deleted files By now, you must have also found your lost file in the list. Use the C option to copy the selected file. This file will later be restored to the location you will specify in the next step.
Step 9: Specify the location where the found file will be restored
Recover deleted files now that we have copied the lost file that we have now found, the testdisk utility will display the following screen so that we can specify where to restore it.
You can specify any accessible location as it is only a simple UI thing to copy and paste the file to your desired location.
Step 10: Copy/restore the file to the selected location
After making the selection about where you want to restore the file, click the c button. This will restore your file to that location:
This might seem to be a slightly long process but it is definitely worth getting your lost file back. The restored file will most probably be in a locked state. This means that only an authorized user can access and open it.
We all need this tool time and again, but if you want to delete it till you further need it you can do so through the following command:
$ sudo apt-get remove testdisk
You can also delete the testdisk.log file if you want. Enjoy getting your lost file back and thank TeskDisk!