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 Debian through the TestDisk utility.
We have run the commands and procedures mentioned in this article on a Debian 10 Buster system.
The Scenario
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 Debian.
Step 1: Install the TestDisk utility
Open your Debian command line, the Terminal, through the system application launcher search as follows:
The Application Launcher bar can be accessed by hitting the Super/Windows key.
Then run the following commands as sudo in order to install the TestDisk utility:
$ sudo apt-get update
And then,
$ sudo apt-get install testdisk
Please note that only an authorized user can add/remove and configure software on Debian. 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
Or,
$ 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:
$ sudo 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.
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 entering 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 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 and left arrow keys and hit Enter. As mentioned in the note in the above screenshot, correct disk capacity must be detected in order for a successful file recovery to be performed.
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 of your 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, Debian. 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:
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 I see my lost file “accidently_removed.docx” in the list. Of course, I intentionally named it 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 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. It is such a relief to get your lost file back!