Suffering a loss of data can be a terrifying experience. Many causes jump to mind immediately – malware attack, consequences of running the wrong command, software-level data corruption, or even your failing hard drive acting up. But you know what’s even worse? Doing the damage yourself by formatting the entire drive, partition, or folder! And while there is certain failsafe put in place on Windows, Mac, and Linux in the form of a Recycle Bin/Trash Can, accidents can easily happen. One of the most common causes for accidental deletion on Linux based operating systems is the rm command. So, with that out of the way, here’s how to recover a file deleted with rm.
What is an rm command?
The rm command, which stands for remove, is used for removing files, symbolic links, and directories from UNIX-based file systems, most commonly Linux, Fedora, Manjaro, and Ubuntu. Here is how it works with three files named one.txt, two.txt, and three.txt. You can also replace the names with file paths to directories to delete folders.
Deleting one file
- Open Terminal (On Ubuntu, click the Ubuntu icon in the top-left corner, and type terminal. Or, use the keyboard shortcut Ctrl + Alt + T).
- Enter rm one.txt into the Terminal.
- Press Enter.
Deleting multiple files
- Open Terminal.
- Enter rm one.txt two.txt three.txt into the field.
- Press Enter.
Why is rm command dangerous?
If you tested this, you would have noticed that the files were deleted without asking for confirmation, and without any notifications. This is because the default rm command is designed to work silently by default, and to only notify the user in case there was an error of some kind. Also, the files weren’t sent to the Recycle Bin/Trash Can but deleted permanently. This can wreak havoc on your operating system.
Here are two options that won’t be of use after the files are lost, but will act as a safeguard for the future.
- Option 1. Use interactive deletion. It will ask you to confirm the delete process for every single file by pressing Y on your keyboard. Simply type rm -i one.txt into the Terminal.
- Option 2. Download and install the rm-trash utility. It acts as a prevention from accidentally deleting files by guaranteeing they’ll be sent to the Trash Can. After installing the utility, you can type rm-trash one.txt into the Terminal to delete a file.
Is file recovery possible after an rm command?
It could very well be – there are a lot of factors involved. We’ll try and give you a few tips.
- After you run an rm command, leave the drive be. Do not download, copy, or install anything new, as you risk overwriting the important data.
- Power down the system or unmount the partition. This might not be useful if you already suffered data loss but might save you from a disaster in the future. This is especially useful if you accidentally run an rm command on the entire drive or a large partition. Since there will be a certain delay before the command is “propagated” to the disk, it is possible to cancel the process by interrupting it.
- Try and remember if you created a backup. Even if you get files that are a bit old, you’d still be better off than if you had an empty drive.
- Hire professionals. We’ve already discussed data recovery services and their benefits. If you have important files that you cannot afford to lose, paying someone with decades of experience gives you the highest chance of recovery.
How to restore files deleted with rm command using Terminal
Finding files of different formats other than text makes things very difficult. Here’s how to recover a text file, which will still require a lot of browsing around.
- Open Terminal.
- Type init 1 and press Enter. This will switch the system to a single user mode.
- In most cases, you’ll use this command. Type grep -i -a ‘MyFile’ /dev/[partition-name] > file.txt
- -i will ignore case distinctions e.g. match uppercase and lowercase characters
- -a will process any binary file as if it was a text
- MyFile indicates the word your desired file starts with. Replace it with the one that matches yours
- Replace [partition-name] with the partition the text files are on. For example, /dev/sda2
How to recover files on Linux using data recovery software
Using terminal without any additional help to browse for files other than plain text is not worth the effort. Instead, we recommend using software on our list of best data recovery software for Linux and Ubuntu. We’ve handpicked some, to show you how they work.
TestDisk and PhotoRec are both from the same “family” of free, open-source software and are often used together. Plus, they are both lightweight and portable and thus require no installation. And, just so you know, Linux uses various file systems, most often ext2, ext3, and ext4.
- Download TestDisk for Linux and extract it from the archive.
- Once you open the program, you’ll be met with a window similar to a Terminal, although with a primitive user interface.
- Scroll down using your keyboard and choose Advanced.
- Find the partition/drive that the lost data is on, and choose Undelete. Press Enter.
- After the process is complete, you’ll see deleted files in red.
- As a safety feature, TestDisk doesn’t “recover” files, but rather chooses to copy them to a separate partition for safekeeping. Once you highlight a file, press C on your keyboard to copy the file.
- Use the arrow keys as described to navigate through directories until you find a file path to copy the recovered files to.
- Download PhotoRec for Linux from the same link above. (TestDisk and PhotRec come bundled in the same archive).
- Extract the archive and run the program.
- Choose media (drive, partition, USB drive, SD card, etc.) from the list by using the arrow keys, and press Enter.
- Highlight the ext file system.
- Navigate to File Opt using the arrow keys and press Enter. Enable or disable the extensions of file types you want PhotoRec to search for.
- Once you proceed, you will be asked to confirm the file system. Choose EXT2/EXT3.
- Pick between scanning unallocated files only (recently deleted files) or the whole partition (time-consuming but thorough).
- Pick the output folder/partition for the recovered files on your drive.
- Give the software some time to finish. You can track the progress and see the total number of files it recovered sorted by file extensions.
- Highlight Quit once the search is complete, and press Enter.
How to backup files on Linux
Once you got your files back, make sure you don’t go through the same stress again. This is done through frequently backing up your files, often to an external HDD. The process can also be automated. Linux has a rsync command that can be entered into Terminal and via various parameters, unlock a full set of features. We think having a graphical user interface (GUI) to the command is much better, and recommend luckyBackup.
- Open Terminal.
- On Ubuntu, type sudo apt-get install luckybackup
- Choose a name for the backup and pick Backup Source inside Destination as the Type.
- Pick the Source (the folder you want to back up, or an entire partition) and Destination (your external HDD/USB flash drive).
- Put checkmarks in front of the types of files you want to backup.
- You can choose to create multiple snapshots. Those are “points in history” that you can reroll your files to at will, for example, a certain day and/or time of the day, depending on the configurations.