DEV Community

Cover image for Playing with fire: a devastating command line command (rm)
RealToughCandy.io
RealToughCandy.io

Posted on

Playing with fire: a devastating command line command (rm)

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.

rm command

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.

Seriously. rm should be used sparingly and soberly.

(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.

Chess app screenshot

No worries, 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.

Here is what each command stands for:

  • sudo= superuser do (gives you read/write/superpower abilities)
  • rm = remove
  • -rf = two flags smashed into each other for efficiency, r = “recursive” and f = “force.”

With -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).

Let’s do another one.

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: rm *dorkasaurus*
Bye bye, all you dorkasaurus (but not “dorkasarus”) files.

removing 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.

Remember, there’s no undo button with rm.

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:

man pwd
man ls
man rm

manpages

manpages is essentially a guide explaining what each command and its flags do.

In summary, rm is the most devastatingly powerful command line command that exists.

Whether you use “vanilla rm” or you use it in combination with flags
such as -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.

Happy hacking!

P.S. Follow me on YouTube where I talk a lot about cool web dev stuff:

Top comments (3)

Collapse
 
curtisfenner profile image
Curtis Fenner

At the end of a long day, after writing a script to go through some log files, I was finally done and needed to rearrange a bit of mess of files I had made before heading home. That's when I typo'd rm instead of mv, one of those things your fingers type with you realizing it. I was tired, and never felt more embarrassed at work, because one of my coworkers was watching as I did this.

At least writing the script was easier the second time.

Collapse
 
realtoughcandy profile image
RealToughCandy.io

Eeeeek! I'm glad you lived to share the story though! 😁

Collapse
 
craigmc08 profile image
Craig McIlwrath

I have a /dev folder where I just have all my projects in their own folder. I wanted to delete one of them, so I used rm and somehow deleted everything in my /dev folder... luckily I use git for most of my projects. But I still lost a lot and am now much more cautious with rm.