When you want to perform a certain task on your computer and then you get that pop-up box that asks you if you really want to do what you said you want to do, some people might get agitated. Their tempers might raise over the ceiling and they might even get goose bumps. All because the computer is watching your back.
Enter the Linux command. In this dark screen, the computer assumes you know what you want, and that you know what you are doing. There are no warranties, no guarantees, no insurance. If you screw up, it's entirely and purely your fault. This is the only place where the computer is "always right".
rm -r is the most notorious command-line program I know. You might want to pray to the gods of the Linux terminal before you press enter because once done, there's no going back.
rm stands for remove and the
-r option means recursive and collectively the command tells the computer to remove all files in a directory and it's sub-directories.
You might try to dodge it, work around it, but somehow you won't escape using it if you work in the command-line. That's because you want to delete some files maybe because you no-longer need them or maybe to create space on your limited hard drive.
rm -r is merciless is because it won't prompt you for a confirmation of deletion of files and directories. And that's not the worst part because your ordinary GUI program will at least move files to Trash. rm does NOT send your files to the recycle bin. So if you mistakenly delete files and you don't have backup, that's it. You are screwed. Your best bet is now using recovery software which is a messy job. As a result, this command is notoriously known for ruining people's careers and for some spending long sleepless nights in attempts to reverse rm's damages.
The strangest thing I discovered is that you can actually execute
rm -rf /. That's one sure way of sending your Linux computer with all your data straight to Hell. The Linux file system follows a hierarchical structure with "/" being what's called the "root". Ordinarily everything your computer needs to run well stems from there; critical system files, binaries/executable programs, configuration files, application data and your home directory that has your documents, downloads and cat Videos. So
rm -rf / deletes everything. All of it. Like those self-destructing Impossible Missions Force (IMF) messages sent to field agents you have heard in Mission Impossible movies.
Here's an experience I had recently. I was moving some files around, actually multiple linux distros I had downloaded. So here I am checking if the folder has data with ls. ls, tells me there's nothing. Then to be safe, I use rmdir which specifically deletes empty directories. Command fails because, it seemed there was data in the folder. So I double check again with ls which once again tells me there's nothing. So I assume there are small hidden files which are probably insignificant. I decide to call the cops rm -f. Then, I discover later than actually the folder had my iso files :( Lesson learnt. Never trust ls.
So how do you deal with this command? I wish there was one sure answer other than to simply be careful. That's all be careful. Like crossing the road, as kids we were given simple instructions that saved our lives; look to the left, then to the right and the to the left again.
rm isn't any different from crossing the highway. Write your command, then examine it carefully to ensure that it's going to do what you actually want it to do before finally hitting enter. Like carpenters, measure twice, cut once.
I wish rm had a --dry-run option which should give the user an option of running the command without affecting anything first. But it doesn't. So for this, I usually use the echo command. So I usually echo out my command first especially when it has variables.
$for i in $(cat my-file-names); do echo "---rm -r /some/dir/ $i---" done; #outputs ---rm -r /some/dir/file-to-del---
After that, once I concern that am deleting exactly what I want to, then I remove the echo command and leave the command. It works for me well.
Some times I also tend to use sleep especially in cases where loops are concerned. I usually sleep for at least 2 seconds which gives me enough time to see what the program is doing. This isn't bullet proof of course like echo, but at least I can handle less damage than all damage.
$for i in $(cat my-file-names); do rm -r /some/dir/ $i; sleep 2; done
So that's how I do. rm is a great program but only when used with caution. If you know of any ways you do it, let me know in the comments below.
Subscribe to David Okwii dev blog
Get the latest posts delivered right to your inbox