We all know the feeling of losing a file, looking for it in the trash, and not even finding it there. Luckily the trauma is temporary and there are various tools available through which you can get your lost file back. Most of these tools recover the lost files from the previous images(memory state) of your system. One of such tools is the tried, tested, and extremely dependable TestDisk utility. 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.
In this article, we will explain how to recover your accidentally lost files on Ubuntu through the TestDisk utility.
We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.
So, the situation is that I mistakenly deleted, even from the Trash can, a document file (.docx) that was present in my /home/[user]/Downloads folder. Now I have to find a way out through which I can restore the file to its previous location. Or at least restore it to any location where I can access it and then move it to my required directory.
Here is a step by step process that will help me, and also you, in restoring a mistakenly deleted/lost file in Ubuntu.
Step 1: Install the TestDisk utility
Open your Ubuntu command line, the Terminal, either through the system application launcher search or through the Ctrl+alt+T shortcut.
Then run the following command as sudo in order 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 after which the utility will be installed on your system.
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
$ testdisk -v
Step 2: Run testdisk and create a new testdisk.log file
Use the following command in order to run the testdisk command line utility:
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.
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.
Important: 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 hitting 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.
Step 3: Select your recovery drive
The utility will now display a list of storage drives attached to your system. In my case, it is showing my hard drive as it is the only storage device on my system.
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 a 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
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 (sana):
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
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
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.
I am specifically selecting the location from where I lost the file, my Downloads folder:
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:
See the text in green in the above screenshot? This is actually great news. Now my file is restored on the specified 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!