Stop Using “rm” on the Command Line (Before It’s Too Late)

True story…

While writing a Python script one day to do a little screen scraping and reporting, the topic of data loss came up between a friend and myself. I was bragging that I had never in my life accidentally lost a single file or piece of data that I wasn’t able to recover. Literally seconds later, while intending to rename my script using “mv”, I accidentally typed “rm” instead and deleted it.

That was just about the last time I used “rm”. I now use a script which, for no particular reason, I call “rr”. Rather than deleting files and/or directories, it moves them to the trash where they can be easily recovered if necessary. Here’s the script itself (configured to work on OS X):

#!/bin/bash

if test $1
then
    mv $1 ~/.Trash/
fi

Just make the script executable (chmod 755 rr), drop it in your path, and forget “rm” ever existed.

As always, there are several other ways of doing this (remapping “rm”, making an alias, using the Finder, etc.), so pick the way that works best for you. The important thing is to keep yourself from losing hours of work on the command line like I did.

3 Responses to Stop Using “rm” on the Command Line (Before It’s Too Late)

  1. I would take it an extra step to by adding an alias to your ~/.bash_profile

    alias rm=”echo ‘use rr instead'”

    source ~/.bash_profile

    Next time you try to run the command rm, you will get a little warning to use rr instead. This will completely avoid you using rm by accident since rr is so close to rm rather than mv.

  2. definitely a good tip.
    thanx

  3. Blow Toad says:

    How about just using this:
    alias rm=’rm -i’

    so that rm prompts you to see if you really want to delete the file(s). If you are sure that you want to, you can always use the -f to force it to not ask.

    And when you do use -f, always put it at the end like
    rm mydir -r -f.

    Then you don’t acidentally hit enter on
    rm -r -f mydir1
    when meaning to remove mydir12.