Ninth Command: Behold, the Destroyer
There is a rite of passage for those who seek to enter geekhood. It’s not being able to sit at a keyboard and counter the efforts of someone trying to crack your system; it’s not being able to create a new user or manage multiple devices. The rite of passage for geek candidates is the following:
host% rm *
Most geeks, at one time or another, have unintentionally typed this simple, innocuous phrase in a location that will cause them some discomfort. It’s through this experience that the geek receives a demonstration of the greatest risk to most Unix systems…ourselves.
The simple ‘rm’, is the Unix Remove command and is used to remove a file or directory from the filesystem. It’s essential to keep a directory free of no longer wanted files or directories, and without it, eventually you’ll use up all your space and not be able to add new and more exciting material. However, it is also the command that most people use incorrectly at some point, much to their consternation.
To remove a specific file, type ‘rm’ with the filename following:
host% rm filename
To remove an entire directory, use the following, the -r flag signaling to recursively traverse the directories removing the contents in each:
host% rm -r directoryname
When removing an entire directory, you’ll be prompted for each item to remove, and this prompt can be suppressed using the -f option, as in:
host% rm -rf directoryname
So far, the use of remove is fairly innocuous, as long as you’re sure you want to remove the file or directory contents. It’s when remove is combined with Unix wildcards that warning signs of ‘Ware, there be dragons here should be entering your thoughts.
For instance, to remove all JPEG files from a directory, instead of removing each individually, you can use a wildcard:
host% rm *.jpg
This command will remove any file in a directory that has a .jpg extension. Any file. Simple enough, and as long as that’s your intent, no harm.
However, it’s a known axiom that people work on their web sites in the dead of night, when they’re exhausted or have had one too many microbrews. Our minds are befuddled and confused and tired and not very alert. We’re impatient and want to just finish so we can go to bed. So we enter the following to remove all JPEG files from a directory:
host% rm * .jpg
A simple little space, the result of a slight twitch of the thumb, and not seen because we’re tired — but the result is every file in that directory is removed, not just the JPEG files. And the only way to recover is to access your backups, or seek the nearest Unix geek and ask them to please, pretty please, help you recover files you accidentally removed.
And they’ll look at you with a knowing eye and say, “You used rm with a wildcard, didn’t you?”
Which leads us to our last Command, and the most important…