Blog

Idiot-proof ‘rm’

After my unfortunate accident, I decided to spend a moment trying to make sure it doesn’t happen again.
Thus, the (semi) idiot-proof ‘rm’ was born:

This is a small script that replaces ‘rm’. Instead of deleting, it moves the files in question to ~/.Trash.
It’ll create a directory structure under ~/.Trash that echoes that of the current working directory, so it’s easy to see where files came from.

Here’s the script:

#!/bin/sh

if [ “$1” = ‘-rf’ -o “$1” = ‘-r’ ]; then
   shift;
   recursive=true;
fi;

while [ “$1” ] ; do
   # If not recursive, skip directories
   if [ -d “$1” -a ! “$recursive” ]; then
      echo $
0: $1: is a directory; shift; continue;
   fi;

   [ ! -d ~/.Trash/
“`pwd`” ] && mkdir -p ~/.Trash/“`pwd`”;
   mv
“$1” ~/.Trash/“`pwd`”;
   shift;
done;

trash.sh

To use it, save it into a file (I use ~/.trash.sh), then save and set it to executable. Then, open up ~/.bash_profile, and add the line:

alias rm=’~/.trash.sh’

That will automatically be run when you log in. To make it work in the current login, just use:

source ~/.bash_profile

…Or type the alias command straight in.

. Bookmark the permalink. Both comments and trackbacks are currently closed.