Join Nostr
2025-11-21 01:50:24 UTC

Andrey {.ru account}; on Nostr: Failsafe rm, chmod & chown Don't let a tiny typo ruin your system! Put these aliases ...

Failsafe rm, chmod & chown

Don't let a tiny typo ruin your system!
Put these aliases into bashrc/zshrc before you experience an accidental chmod 777 / by yourself.

For GNU coreutils:alias rm='rm --preserve-root -v'
alias chmod='chmod --preserve-root -v'
alias chown='chown --preserve-root -v'


For Busybox that doesn't support this flag:# /etc/bash/bashrc or ~/.bashrc

real_rm=$(which rm)
real_chmod=$(which chmod)
real_chown=$(which chown)

_preserve_root () {
for i in "$@"; do
# remove meaningless cwd dots and double slashes
i=$(echo -n "$i" | sed -E -e 's#/\.(/|$)#/#' -e 's#/{2,}#/#')
# if arg does not point to root, go on
[ "$i" = "/" ] || continue
# otherwise return error
echo 'Operation not permitted on root' >&2
return 1
done
return 0
}

rm () {
_preserve_root "$@" || return 1
"$real_rm" "$@"
}
chmod () {
_preserve_root "$@" || return 1
"$real_chmod" "$@"
}
chown () {
_preserve_root "$@" || return 1
"$real_chown" "$@"
}


RE: https://gts.dc09.ru/@darkcat09/statuses/01K0RNM6CBRQHPAB1VK0QJS32Y

#snippet #linux #bash #typo