Skip to content

Instantly share code, notes, and snippets.

@rcmdnk
Last active December 23, 2022 16:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcmdnk/5439030 to your computer and use it in GitHub Desktop.
Save rcmdnk/5439030 to your computer and use it in GitHub Desktop.
rm wrapper to protect HOME and / directory. To use, put this gist in your .bashrc or .bash_profile.
#alias rm="rm -i"
#function rm={ rm $@ -i; }
function rm {
for f in "$@";do
if [[ "$f" =~ ^- ]];then
continue
fi
if [ "$f" = "$HOME" ] || [ "$f" = "$HOME/" ];then
echo -n 'Are you sure to remove your HOME? '
read yes
if ! [[ "$yes" =~ ^[yY] ]];then
return 0
fi
elif [ -d $f ];then
dn="$(cd "$(dirname $f)";pwd -P)"
if [ "$dn" = "$HOME" ] || [ "$dn" = "$HOME/" ];then
echo -n 'Are you sure to remove your HOME? '
read yes
if ! [[ "$yes" =~ ^[yY] ]];then
return 0
fi
fi
fi
done
command rm --preserve-root $@
#command rm -i --preserve-root $@
#command rm --preserve-root $@ -i
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment