Skip to content

Instantly share code, notes, and snippets.

@kuleszaj
Created April 29, 2012 01:43
Show Gist options
  • Save kuleszaj/2523251 to your computer and use it in GitHub Desktop.
Save kuleszaj/2523251 to your computer and use it in GitHub Desktop.
Safe Remove
#!/bin/bash
sleep 2
args=( $@ )
preserve_paths=( / /bin /boot /dev /etc /home /initrd /lib /proc /root /sbin /sys /usr /usr/bin /usr/include /usr/lib /usr/local /usr/local/bin /usr/local/include /usr/local/sbin /usr/local/share /usr/sbin /usr/share /usr/src /var /opt )
preserve_keywords=( bin boot dev etc home initrd lib proc root sbin sys usr include local share src opt var )
for path in "${preserve_paths[@]}"
do
for arg in "${args[@]}"
do
if [[ $arg = $path ]];
then
echo "I refuse to delete: $path"
echo "It seems too important. If you want to do it anyway, use /bin/rm"
exit 100
fi
done
done
counter=0
matches=()
for keyword in "${preserve_keywords[@]}"
do
for arg in "${args[@]}"
do
if [[ $arg = $keyword ]];
then
counter=$(($counter + 1))
matches=( ${matches[@]} $keyword )
fi
if [[ $counter -gt 1 ]];
then
echo "I refuse to delete: ${matches[@]}"
echo "They seem too important. If you want to do it anyway, use /bin/rm"
exit 100
fi
done
done
/bin/rm "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment