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
rm
to its interactive mode, which will force you to step through what it's trying to delete:alias rm='rm -i'
-
Install
coreutils
which includes a newer version ofrm
with the flag--preserve-root
which is enabled by default and will prevent you from removing root. OS X:brew install coreutils
Linux:apt-get install coreutils
With this version of
rm
you 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-rm
which will let you set off-limits directories. -
ZSH users can also do the following:
-
Put
unsetopt RM_STAR_SILENT
in your .zshrc, which will make it ask you before executingrm
with a starrm folder/*
. -
Put
setopt RM_STAR_WAIT
in your .zshrc, which will make it wait 10 seconds until executingrm
with a starrm folder/*
.
-
rm -rf --no-preserve-root /