rm -rf
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.
function rm () {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.
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
}
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.
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.