Saturday, July 7, 2007

A Smarter rm for Mac users

Hmm, today I had a nice experience. I wanted to remove two files; httpd and httpd.tar. The former being a directory.

Did a rm -r httpd *

Went away, then returned, panicked, seeing that the space between the httpd and *.

Well, there went a good part of my documents. The important things I tend to replicate everywhere, but now I'm hunting lego manuals. From this experience came out a nice little script for all my fellow OS X users. All OS X users, who, like me, don't want to say "y" to every file during an rm.

First, open your .profile and add in the following line:

alias rm '~/rm-trash'

Second, open the terminal and type the following:

$ touch rm-trash

$ chmod 755 rm-trash

open the empty rm-trash file in your favorite editor, and type up the following:


#!/bin/bash

if [ "$#" -eq "0" ]; then
echo "No files specified for deletion."
else
for i in $*
do
echo "Trashing: $i"
mv "$i" ~/.Trash
done
fi


There, my little magic script to forward my rm's to the trash; so from the command line, if I make a mistake, it's not as fatal. I can still get my files back. And if I didn't make a mistake? Just open the apple menu, and do an empty trash.

Just call it like:

$ rm "Some Folder" "Some File"

No need the -r, or things like that. But you need quotes. Bash handles wild-cards, so:

$ rm "Some*"

would remove both files "Some Folder" and "Some File". Put in the quotes! Or else bash may treat the sections of the file name divided by spaces as different parameters.

This should work for Gnome/KDE users as well - I believe that a folder is used for the trash as well.

No comments:

Post a Comment