Loss of data is a terrifying experience. Sometimes it is accidental and sometimes intentionally. There are even some cases when this happens due to malware attacks, software level corruption, or hard drive failure.
But when it comes to Ubuntu, running the wrong command adds on. If you have done the same by using the rm command and are now looking for a solution. Just follow this complete guide on how to recover deleted files by rm command on Ubuntu?
You will learn easy and tested techniques to recover rm files on Ubuntu.
rm stands for remove here and is basically used for removing objects. Objects like files, directories, symbolic links, etc from the file system such as UNIX.
The main function of the rm command is to remove references to objects from the file system. This command works silently so you should take utter care while running this command. This is so because once you delete files, it becomes difficult to recover the contents of files and directories.
Syntax: The syntax for the “rm” command is
rm [OPTIONS]... FILE...
Deleting single file: If you want to delete a single file, you have to use the “rm” command followed by the name of the file as an argument.
Deleting multiple files: If you want to delete multiple files you just need to pass the filenames as arguments separated by space.
rm filename1 filename2 filename3 filename 4
Removing directories: If you want to remove 1 or more than 1 directories you have to use “-d”
rm -d dirname
How does it make you feel when you accidentally delete some important files? Obviously, you will see your efforts going in vain.
But we don’t let it happen to you. You can easily recover rm files on Ubuntu using TestDisk. You just need to follow some simple steps.
Step 1: Install the Testdisk tool on your system, if your Linux distribution doesn’t have it in the official repository. If you are using Ubuntu or other Ubuntu-based Linux distribution you just need to type the command line.
sudo apt install testdisk
Once you are done with the command you have to run TestDisk in the terminal. For this follow the command
Step 2: On opening, you will see a screen as shown. Now use the arrow keys to navigate followed by “enter” to select.
Note: There may be some screens that present you with extra commands. If you are using TestDisk 7.0, the next reasonable step will be highlighted. This will provide you the right guidelines but you are advised to read yourself and then proceed ahead.
Step 3: This is where you will see your drive. If you are not able to see it, then the ownerships can get blurry. In this case, the TestDisk will demand your permission to open them.
Now all you need to do is to select “Sudo” and enter your password. Once entered correctly just hit “enter” followed by another “enter” on the next screen. This will let you create another log file.
Step 4: Now you will be able to see all of your drives. Just arrow key to the drive and hit “enter” to proceed.
This time also TestDisk will select the correct settings. Hit “enter” again.
Step 5: If you have gone through the first screen carefully you are all well. If not, you need to know that disk utility is not only for recovering deleted files it has a lot more to offer. You also need to consider that we are not fixing the disk, but we are recovering a file. So select the “advanced” option and hit “enter” to continue.
Step 6: Now you will see “Undelete “at the bottom of the page, select it.
Step 7: Now you will get all of the deleted files highlighted in red. Now all you need to do is to read choices at the bottom after arrowing down to the required file.
Step 8: Now you need to know that most of the options in TestDisk deal with groups of files. What we want is our deleted file back. So now press”c”. This will let you copy and recover the deleted file.
On clicking “c” you will be provided with options about where you want to store the recovered file. By default, the settings will be set to the home folder. Once you are done with selecting the location, your file will be restored to the selected location.
Now select “quit” and hit “enter”. Now again hit “quit “ and “enter”. Finally, repeat the “quit” and “enter” to exit TestDisk completely.
Ext3grep provides you with one of the best solutions to recover deleted files. All you need to do is to follow some simple steps.
Step 1: Use the “apt-get update” and “apt-get upgrade”. This will let you both update and upgrade new versions of packages.
Step 2: Now create an ext3 extension location keeping 400 MB size.
$ sudo dd if=/dev/zero of=/tempfs bs=1M count=400
You can use K, G, T, P, E, Z, and Y instead of M.
$ ls /
$ sudo mkfs.ext3 /tempfs
mke2fs 1.41.3 (17-May-2015)
Discarding device block: done
Creating filesystem, with 409600 1k blocks and 102400 inodes
Filesystem UUID: de4f963a-12c4-4bcf-6586-1bf3366ff94d
Superblock backups stored on blocks:
8193, 24777,45766, 73727, 204771, 40109
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
Step 3: Create a Mounting Point as shown.
$ sudo mkdir /mnt/data
$ sudo mount –t ext3 /tempfs /mnt/data/
$ df –hT
Step 4: Now you have to create data for the test. Once the file system is mounted you need to remove the file immediately on copying.
$ sudo cp –r /etc/services /mnt/data/
$ ls –la /mnt/data/
$ cd /mnt/data/
$ sudo rm –f services
$ cd /
Now you have to unmount the partition. This will save your files from getting overwritten.
Note: Never go with ext3grep for recovery from a mounted filesystem.
Step 5: Unmount/mnt/data. This will prevent overwriting. This will also prevent creating inodes for files to recover in the location.
$ sudo umount /mnt/data
Step 6: Install ext3grep. For this, you just need to follow a simple command on the terminal.
$ sudo apt-get install ext3grep
Now find /tempfs using the est3grep option. Once the file got removed, you have to use the ext3grep utilities “-dump-name” option. This will display a list of file names.
$ sudo ext3grep –-dump-name /tempfs
Step 7: Now restore / locate data in /RESTORED_FILES. To recover deleted files you will be provided with a single file recovery option and all files recovery option.
If you want to recover a single file, you can go with “-restore-file”. On the other hand, if you want to restore all deleted files, you can go with “-restore-all”.
$ sudo ext3grep –-restore-all /tempfs
$ cs RESTORED_FILES/
$ ls –la
When it comes to recovering deleted files there are many ways. But the problem arises when they are deleted even from trash or recycle bin. In another case, there is a lot of software that can restore deleted files. But they fail to recover some type of files
In this situation, you can use Wondershare Recoverit. It is data recovery software that can recover all file types.
You only need to unplug the drive on ubuntu and then connect the drive to a Windows computer. Once connected, follow 3 simple steps.
Step 1: Select a location
Install Wondershare Recoverit on your Windows computer. Log in to Wondershare and select the drive from the main menu. It will be under external devices.
Step 2: Scan the location
On clicking “start”, Recoverit will start an all-around scan of the selected storage device. This will take some time.
You can pinpoint files during this scanning process. You can also start or stop the scan during the process.
Step 3: Preview and Recover Files
You can simply click on “Recover” to continue or you can preview your file.
Once you are satisfied, click on “Recover” to continue. You will be asked for the location. Select the one where you want to store and you are done.
When it comes to recovering deleted files from both Windows and Ubuntu, there are many techniques available online for the same. But what matters the most is reliability. The techniques that are presented to you here are trusted and reliable techniques. If you want an easy technique, you can use Recoverit for the same. It will let you recover deleted files of all types.