Geekblok

B10m, BOK, Joffie - old geeks on a blog

Entries Comments



rm -rf

18 June, 2008 (21:28) | guides, technical | By: Joffie

Every once in the several years I am a little too quick at the keyboard and manage to wipe a part of my home directory. I refuse to alias rm to rm -i because I always know what I am doing (Yeah right).
Yesterday it happened again. I managed to ctrl-c quickly, so I didn’t loose to many important files. It only got ~/bin/. Most of those files I already had on a backup machine.

I thought on what I could do to stop this from happening without converting to the alias rm=rm -i. The trick that I am now using is to make rm into a function, which I put into my ~/.bash_profile:
function rm () {
if [[ ${1} =~ -(rf|fr) ]] && [[ ${2} =~ ${HOME} ]]; then
read -p “Hold it, are you sure you want this? [y|N]” I;
if [[ ${I} =~ (y|Y) ]]; then
sleep 5;
$(which rm) $•;
fi;
else
sleep 1;
$(which rm) $•;
fi
}
What this does is question me whenever I am trying to use the -rf or -fr options with rm and that the second argument somehow matches my homedirectory. After I am stating that I am fine with the rm, the function gives me five more seconds to thing about what I am about to do. If still I haven’t hit ctrl-c within those five seconds, I am sure I really meant the rm in the first place.

Before you start using it, make sure that the function is sourced (hint: type rm). I am not responsible if you loose data if you decide to use this function.

Comments

Comment from Joffie
Time: July 24, 2008, 11:44 am

Update: Putting:

export HISTIGNORE=”rm -rf *”

in ~/.bashrc would have saved me from calling ‘rm -rf *’ (I got it via ctrl-r). Everything in the environment variable HISTIGNORE will refrain bash from putting the rm call in the history.

Write a comment





Preview: