The other day I wondered to my self,
What would happen if I run
rm -rf / --no-preserve-rooton my computer?
I soon realized that running
rm -rf / --no-preserve-rooton my computer would be a terrible idea given that it would delete everything. Luckily, virtual machines exist and in a few minutes, I was able to install a Ubuntu VM and delete everything in there instead.
What Does rm -rf / –no-preserve-root mean anyway?
rm -rf / –no-preserve-root does the following:
- rm means delete something
- -r means recursive, allowing us to delete a directory
- -f means force delete, that means that there will be no output to the terminal regardless of whether the deletes passes or fails.
- / is the root directory
- –no-preserve-root tells the rm command that we definitely want to delete root since rm is smart enough to deny us the ability to delete root, unless we tell it to.
What happens when we actually run it?
In the video above, we initially get lots of Permission Denied and Operation not permitted errors. Sometime later, the side dock loses the non active applications before going away altogether. Shortly afterwards, the sound switches off before finishing off the delete.
The effect of running the rm -rf / command are as follows:
- While the terminal is still running, none of the Linux commands work. Things like rm, vi, cat, ls, root etc. are considered invalid commands. cd also fails with the exception of switching to the root directory.
- The ssh connection I had running was still live and I could log out of it, I just couldn’t log back into it.
- The text editor worked, although I could not save anything and when I wanted to close, instead of the save dialogue, I get a prompt saying that the dialogue could not be found.
- My text editor, Visual Studio Code, kept working, although the terminal wouldn’t let me run commands anymore.
- My web browser still worked and had the connection. The catch is that some fonts stopped working, meaning that I could not see any text in the console, view source, inspect element and other fonts I would otherwise find easily. I would guess the only fonts I could see were the ones that the webpage loaded externally, explaining why the monospace fonts in Stack Overflow didn’t work, but the ordinary font did.
- I was not able to pass the shutdown signal from either the terminal or my VM. I had to shut down by “pulling the plug” from the Windows host.
- When I tried to restart, I only got grub rescue, which I didn’t know how to use, meaning that I can safely conclude that rm -rf / does delete everything. The only reason I could see why some things sort of worked was because they were loaded into memory, meaning that they weren’t on the hard drive.
So there you have it! If you run rm -rf /, bad things happen. Fortunately, this is only disastrous when ran as root (I tried it without sudo and everything was fine after a restart) and with the –no-preserve-root. If you want to try this command for yourself, all you need to do is get Ubuntu, or your favorite flavor of Linux and Virtual Box for the VM to run the command.