Let’s talk about the most dangerous, powerful, and perhaps even most exciting command line command of them all: rm.
rm is shorthand for “remove.” At its core,
rm allows you to delete things with unmatched precision and speed.
OK, great — so it removes things. Big deal, right? Can’t you just do the same thing with the trash can button? Why yes… yes you can. What makes
rm terrifying, however, is that once you run that command, there is no “undo” button.
It’s gone forever.
When you read forum posts involving the
rm command, battle-hardened devs will wisely open up their posts with sage, stern, and bold words of warning: never run this command unless you know exactly what you’re doing with it.
Now, to echo the wise and give my own sage and stern word of warning before I start this demo: use
rm sparingly and never run this command unless you know exactly what you’re doing with it.
(For this demo, we’re in a controlled environment; I also understand the risks involved, so let's proceed!)
Let’s say I want to remove one of these stubborn apps that insists it needs to stay on my machine because it’s an integral part of my operating system. Chess.app, really? Why don’t I believe you? I can’t drag and drop you to the trash — why? You’ve got to go.
rm is here to help.
Navigating to the Applications folder on the command line, we’ll then type:
sudo rm -rf Chess.app/
And into dust it returns.
sudo= superuser do (gives you read/write/superpower abilities)
-rf= two flags smashed into each other for efficiency,
r= “recursive” and
-rf, we’re telling it to delete everything starting at that directory, working all the way down into its subfolders, subfolder subfolders, subfolder subfolder subfolders, and alllllllllll the way down until everything is nuked into oblivion (recursive) without prompting for confirmation (force).
Say I have a bunch of files in a directory that contain the word “dorkasaurus” that are no longer needed. We'll type
rm, then pass in our keyword contained by asterisks:
Bye bye, all you dorkasaurus (but not “dorkasarus”) files.
This command can be much faster than deleting files and directories manually. As you can imagine, this command and its assorted flags can also really mess up your system and ruin your week.
One safe alternative you may consider using is a utility like trash-cli, which sends your files to Trash so you can manually delete them later.
Also, if you ever want to know what each command does along with its options, you can summon something called manpages using the
man command followed by the command you’re curious about. For example:
manpages is essentially a guide explaining what each command and its flags do.
Whether you use “vanilla rm” or you use it in combination with flags
-rf, it should always be used with caution rather than reckless abandon. If you’re going to experiment with it, consider using a Virtual Machine or some other computer where it’s OK to break things forever.
P.S. Follow me on YouTube where I talk a lot about cool web dev stuff: