Musings of an anonymous geek

March 14, 2007

Safety Precautions When Using the ‘rm’ Command

Filed under: Linux,Scripting,Sysadmin,Technology — m0j0 @ 11:47 am

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!

Technorati Tags: , , , , , , , , , ,

Social Bookmarks:
About these ads

10 Comments »

  1. Bash users can do something similar.

    ls -l *.php

    [ output of files ]

    rm -f !$

    As *.php is the last argument on the line above, the command will be expanded to ‘rm -f *.php’

    — andyd >> http://www.andyd.net

    Comment by andyd — March 14, 2007 @ 12:42 pm | Reply

  2. […] just found a blog post of another writer who (appears) to also be into sysadmin w/ Linux. I enjoyed reading how he makes sure what he is […]

    Pingback by Word Welding » rm goodness — March 14, 2007 @ 1:07 pm | Reply

  3. […] just found a blog post of another writer who (appears) to also be into sysadmin w/ Linux. I enjoyed reading how he makes sure what he is […]

    Pingback by rm goodness « Rural Admin — March 14, 2007 @ 1:09 pm | Reply

  4. I think to be extra safe you should place two dashes between the command switches and the arguments. For example: rm -f — *.php . A more illustrative example of the dangers of rm is at the following url: http://gnuisance.net/tmp/7d/noooooo.txt

    -Dave

    Comment by David A. Harding — March 14, 2007 @ 4:35 pm | Reply

  5. […] just found a blog post of another writer who (appears) to also be into sysadmin w/ Linux. I enjoyed reading how he makes sure what he is […]

    Pingback by Wrap Margin » Blog Archive » rm goodness — September 19, 2007 @ 12:45 pm | Reply

  6. Good stuff. This is called the “quick substitution” event designator: http://www.gnu.org/software/bash/manual/bashref.html#Event-Designators

    Comment by Adam Backstrom — May 27, 2009 @ 7:12 am | Reply

  7. […] I have a few little rituals before running risky commands – like doing a triple take check of the server I’m on. Here’s an interesting article on rm safety. […]

    Pingback by Command line safety tricks Drija — November 15, 2010 @ 3:54 am | Reply

  8. […] I have a few little rituals before running risky commands – like doing a triple take check of the server I’m on. Here’s an interesting article on rm safety. […]

    Pingback by Command line safety tricks - Admins Goodies — August 24, 2011 @ 5:55 pm | Reply

  9. […] I have a few little rituals before running risky commands – like doing a triple take check of the server I'm on. Here's an interesting article on rm safety. […]

    Pingback by Command line safety tricks | PHP Developer Resource — May 2, 2012 @ 1:17 pm | Reply

  10. […] I decided to fix the original issue and keep Linux from messing with drive letters. Most people won’t experience this behavior because they have all disk in a single controller, while this specific machine had to separate controllers. In the old days, I learn that Linux devices were initialized at boot in a sequential way. Today for speed consideration they are initialized in parallel; so I suppose, who gets first gets /dev/sda, and so on. This has nothing to do with BIOS order. That one decides who’s first in every controller, but says nothing on who’s the first controller on the line, the one whose disks take /dev/sda and so on. I decided to use udev rules to fix this, indirectly. I can’t assign drive letters themselves (and if I could, I wouldn’t want to mess with my OS initiatives), but I can assign a Symlink. So I created an udev rule in /etc/udev/rules.d/ with an high priority (but no so high, it’s a symlink, I am not attempting to change drive letters/devices). SUBSYSTEM=="block", SUBSYSTEMS=="scsi", ATTRS{model}=="ICXXXXXXXXXXXXXXX", SYMLINK+="diskone%n" SUBSYSTEM=="block", SUBSYSTEMS=="scsi", ATTRS{model}=="WDCXXXXXXXXXXX", SYMLINK+="disktwo%n" the rules just looks for hardisk with the specific models and assigns a symlink and restarted udev. I had just two different disks, with no specific serial or other peculiar characteristic other than  model and size showing in udevadm info -a -p $(udevadm info -q path -n /dev/sdg) Having identical disk on this one could be very annoying. I now have a symlinked device like /dev/diskone and used that in /etc/crypttab without any issue. In the process of discovering the information required for this fix, I got two interesting articles (unrelated) Hole hawg  and an interesting overview on the danger of rm Safety Precautions When Using the ‘rm’ Command. […]

    Pingback by Cryptswap and Linux assigning different devices letters to disk | Napalmpiri — September 27, 2012 @ 4:01 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: