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.
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 rm -i. Just doing this step is not enough as -f overrides -i.
If you only want to alias rm in your interactive shell (and not in scripts), just add alias rm='rm -i' to your .bashrc or .zshrc.
If you want to force scripts to use rm -i as well, follow these steps depending on which shell you're using:
bash:
Add alias rm='rm -i' to your .bashrc.
Create a file called .bashenv in your home directory with shopt -s expand_aliases and source .bashrc.
In your .bashrc, add export BASH_ENV='~/.bashenv'.
zsh:
If you use zsh and want rm to be aliased in bash scripts, zsh scripts and your own interactive z shell, we have to jump through some more hoops:
Create a file called .common_profile in your home directory with alias rm='rm -i'
Create a file called .bashenv in your home directory with source ~/.common_profile and shopt -s expand_aliases.
Create a file called .zshenv in your home directory alongside .bashenv and add source ~/.common_profile.
Finally, in your .zshrc add source ~/.common_profile and export BASH_ENV='~/.bashenv'. Your .common_profile, .bashenv, .zshenv and .zshrc should end up looking like this.
Install coreutils which includes a newer version of rm with the flag --preserve-root which is enabled by default and will prevent you from removing root.
macOS: brew install coreutils
Linux: Included by default.
With this version of rm you can also choose to switch from alias rm='rm -i' to alias 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 executing rm with a star rm folder/*.
Put setopt RM_STAR_WAIT in your .zshrc, which will make it wait 10 seconds until executing rm with a star rm folder/*.
How not to
rm
yourselfThe
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.Use
trash-cli
The
trash-cli
command-line app will move stuff to the trash instead of permanently deleting it:Works on macOS, Linux, Windows. Requires Node.js.
Safeguard
rm
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 moderm -i
. Just doing this step is not enough as-f
overrides-i
.If you only want to alias
rm
in your interactive shell (and not in scripts), just addalias rm='rm -i'
to your.bashrc
or.zshrc
.If you want to force scripts to use
rm -i
as well, follow these steps depending on which shell you're using:bash:
alias rm='rm -i'
to your.bashrc
..bashenv
in your home directory withshopt -s expand_aliases
andsource .bashrc
..bashrc
, addexport BASH_ENV='~/.bashenv'
.zsh: If you use zsh and want
rm
to be aliased in bash scripts, zsh scripts and your own interactive z shell, we have to jump through some more hoops:.common_profile
in your home directory withalias rm='rm -i'
.bashenv
in your home directory withsource ~/.common_profile
andshopt -s expand_aliases
..zshenv
in your home directory alongside.bashenv
and addsource ~/.common_profile
..zshrc
addsource ~/.common_profile
andexport BASH_ENV='~/.bashenv'
. Your.common_profile
,.bashenv
,.zshenv
and.zshrc
should end up looking like this.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. macOS:brew install coreutils
Linux: Included by default.With this version of
rm
you can also choose to switch fromalias rm='rm -i'
toalias rm='rm -I'
which is similar: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/*
.Always backup your system.