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
Published at
2025-11-21 01:50:24 UTCEvent JSON
{
"id": "9b49c7da169ae7fc3b3cff64421aad6b8059565b766cacc4c677cb69fa12d93a",
"pubkey": "f6f166ebd192b98870605a341901110f589528394ca9e10b64719a686dae76fe",
"created_at": 1763689824,
"kind": 1,
"tags": [
[
"t",
"snippet"
],
[
"t",
"linux"
],
[
"t",
"bash"
],
[
"t",
"typo"
],
[
"proxy",
"https://gts.dc09.ru/users/darkcat09/statuses/01K0RRK6H2NPTXF27Y3D5A0450",
"activitypub"
],
[
"client",
"Mostr",
"31990:6be38f8c63df7dbf84db7ec4a6e6fbbd8d19dca3b980efad18585c46f04b26f9:mostr",
"wss://relay.ditto.pub/"
]
],
"content": "Failsafe rm, chmod \u0026 chown\n\nDon't let a tiny typo ruin your system!\nPut these aliases into bashrc/zshrc before you experience an accidental chmod 777 / by yourself.\n\nFor GNU coreutils:alias rm='rm --preserve-root -v'\nalias chmod='chmod --preserve-root -v'\nalias chown='chown --preserve-root -v'\n\n\nFor Busybox that doesn't support this flag:# /etc/bash/bashrc or ~/.bashrc\n\nreal_rm=$(which rm)\nreal_chmod=$(which chmod)\nreal_chown=$(which chown)\n\n_preserve_root () {\n\tfor i in \"$@\"; do\n\t\t# remove meaningless cwd dots and double slashes\n\t\ti=$(echo -n \"$i\" | sed -E -e 's#/\\.(/|$)#/#' -e 's#/{2,}#/#')\n\t\t# if arg does not point to root, go on\n\t\t[ \"$i\" = \"/\" ] || continue\n\t\t# otherwise return error\n\t\techo 'Operation not permitted on root' \u003e\u00262\n\t\treturn 1\n\tdone\n\treturn 0\n}\n\nrm () {\n\t_preserve_root \"$@\" || return 1\n\t\"$real_rm\" \"$@\"\n}\nchmod () {\n\t_preserve_root \"$@\" || return 1\n\t\"$real_chmod\" \"$@\"\n}\nchown () {\n\t_preserve_root \"$@\" || return 1\n\t\"$real_chown\" \"$@\"\n}\n\n\nRE: https://gts.dc09.ru/@darkcat09/statuses/01K0RNM6CBRQHPAB1VK0QJS32Y\n\n#snippet #linux #bash #typo",
"sig": "53d495bc8e40a03c5b67784a06ad3c57c1f70a4f35ac772574cbb2493dc54223e0b59e5221dea42220069d211b2be61bf6fff2a11c38036e1f555f6ddd26a947"
}