Prevent wrong rm -rf

I writted the following function to prevent wrong rm -rf:

function rm() {
rf_detected=0
for arg in “$@”
do
if [[ $arg == -*r*f* ]]
then
rf_detected=1
fi
done
if [ $rf_detected -eq 1 ]
then
echo “-rf detected. Action stoped.”
else
/bin/rm “$@”
fi
}

To use just put it at the end of .bashrc, this way if you use rm -rf you will get a message, so you have to think twice and to really remove use /bin/rm -rf …

Be careful about the use of this function, for example if some crontabs have inside them source ~/.bashrc they will use this function too, so at them you need to use /bin/rm.

Another way (not so good) to avoid this to add at this function a check of $PPID if it belongs to to bash you use, but this can be tricky too, see this example:

eoliveira@local:~$ echo $PPID
1870
eoliveira@local:~$ cat /proc/1870/cmdline
/usr/bin/python/usr/bin/terminator

eoliveira@local:~$ echo $PPID
4119
eoliveira@local:~$ cat /proc/4119/cmdline
gnome-terminal

root@blogs:/var/www# echo $PPID
27808
root@blogs:/var/www# cat /proc/27808/cmdline
sudo-i-u root

You can do in another way and check if the thing running is a crontab.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>