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:


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

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

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

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

alias rm=’~/’

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.