Copied from https://github.com/sindresorhus/guides/blob/master/how-not-to-rm-yourself.md
The rm command is inherently dangerous and should not be used directly. It can at worst let you accidentally remove everything. Here's how you can protect you from yourself.
The trash command-line tool will move stuff to the trash instead of permanently deleting it. You should not alias rm to trash as it will break external scripts relaying on the behavior of rm. Instead use it directly: trash image.jpg.
Use trash instead of rm:
brew install trash
Use trash-cli instead of rm:
apt-get install trash-cli
Use recycle.
Even though you don't use rm directly, external scripts most likely will. There are some things you can do to safeguard this:
-
Alias
rmto its interactive mode, which will force you to step through what it's trying to delete:alias rm='rm -i' -
Install
coreutilswhich includes a newer version ofrmwith the flag--preserve-rootwhich is enabled by default and will prevent you from removing root. OS X:brew install coreutilsLinux:apt-get install coreutilsWith this version of
rmyou can also choose to switch fromalias rm='rm -i'toalias rm='rm -I'which is similar:-I prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes.
-
Install
safe-rmwhich will let you set off-limits directories. -
ZSH users can also do the following:
-
Put
unsetopt RM_STAR_SILENTin your .zshrc, which will make it ask you before executingrmwith a starrm folder/*. -
Put
setopt RM_STAR_WAITin your .zshrc, which will make it wait 10 seconds until executingrmwith a starrm folder/*.
-