Usually, if I have a bunch of files that need to go away, I’ll see what I can do to avoid using ‘rm’. Many times, I can move a directory containing the files out of the way, or I can make a backup directory and move the files there. However, at some point, those files are just taking up space, and need to be removed with ‘rm’. I treat this with a lot of caution.
The first thing I do before running the ‘rm’ command is run ‘which rm’. I’m in an environment where some utilities are in a mounted directory, and they duplicate what’s on the local system. I want to know what I’m using.
If I’m on an unfamiliar system, I run ‘man rm’, make sure that man page refers to the binary from ‘which rm’, and then check to see how it handles symlinks. I have yet to see an ‘rm’ that follows symlinks and removes things referenced by them, but I don’t make assumptions. I used to make assumptions like this until one day I ran ‘chown’ on a Solaris system without ‘-h’ and systems all around the department started having issues because they suddenly couldn’t access what they needed to get their work done. :-/
At this point, I used to *type* ‘rm -i’ using the full path to the directory I wanted to work on (which was confirmed using ‘pwd -P’ just to be safe).
Then I’d take my hands off the keyboard and just sit for a moment. I always do this, no matter how stressful the situation. It’s a weird meditative thing. Running the wrong command, or the right one incorrectly, will only make your day worse, no matter how bad it already is. Sit back, close your eyes, and think about what you’re about to do. Then open your eyes, take note of the directory you’re in, take note of the files in there, take note of what user you’re running as, take note of the command you’re running, inspect it character-by-character, and assuming everything is good, I’d hit enter.
The other day I thought of another safety precaution that, while it changes my ritual, might also save me some time. I had to delete all of the PHP files in a directory. In order to insure that only the intended files got removed, I cd’d to the directory, ran ‘ls -l *.php’ and inspected the output. Carefully. Yep – those are all the files I wanted to delete, so then I did this (in a bash shell, but it works in tcsh, csh, and ksh as well):
^ls -l^rm -f
And that’s it. It removes the possibility of having a typo in the *argument* part of the command, which, when rm is involved, is often what gets you in trouble. If you’ve never seen this notation before, it’s a way to repeat the same command line you just ran, substituting what’s after the first caret with what’s after the second caret. So if I do this:
ls -l *.php
And get the proper output, running
^ls -l^rm -f
Will cause this to be run:
rm -f *.php
Hope this helps!