Monday, August 14, 2006

altering the rm command

Anyone who uses the rm command with frequency will eventually make a mistake and erase a file or directory that had important files. To combat this problem I have written the following shell script:

From the command line type:

nano rmspecial

This will create a new file rmspecial and put you in editor mode
Then copy in the following code which I will explain below.

for i in "$@"
var1=$(echo "$i" | sed 's/\///')
mv "$var1" "/$HOME/.Trash/$var1.`date '+%Y-%m-%d.%H.%M.%S'`"
echo "Moving $i to Trash"

Exit nano and save the new rmspecial script

Now change the permissions on rmspecial so that the script can be executed. (You can use chmod)

If you are using the bash shell find the bashrc file and add an alias:
alias rm="/Users/wferrell/.rmspecial"

Now when at the command line when one uses the rm command they are actually calling the .rmspecial script

Looking at the script we see that instead of erasing the files the script moves the files to the trashcan where they can be easily emptied.

The sed command exists to ensure that directories and files can be removed.

Note that the modifiers of the rm command, for example rm -r, no longer exist.


Post a Comment

<< Home