How to alias rm to always confirm

Confirmation is a weak way to achieve the result you want: not deleting files you didn't want to delete. I can ask you to confirm 10 times in a row, but if since you just asked me to delete mispeled.txt you will not realize your error until after you confirmed it.

Better to use trash or similar command on your system that sends files to the (recoverable) "recycle-bin". There is an RPM build of the trash-cli package at rpmfind.net but I can't vouch for that version. When in doubt build it yourself from the source code.

As noted in the comments it is a bad idea to alias rm at all, because it will come back to bite you when you are in a shell that has no protective alias and your brain is accustomed to having a "safe" rm.


In your ~/.bashrc, you can just do this instead:

alias rm='rm -i'

This way, when you type rm -rf example-dir, Bash translates it to rm -i -rf example-dir.

Note that for interactive login shells, ~/.bash_profile is used instead. To make login shells also use ~/.bashrc, simply add this to your ~/.bash_profile:

[ -f ~/.bashrc ] && . ~/.bashrc

Now ~/.bashrc will always execute any time you open a terminal or ssh session.


f --> force, never prompt

i --> prompt every time

If you need to be prompted, just use rm -i in the alias. You could have 2 aliases (rmf and rmi) if you wish to have both.

Tags:

Bash

Alias

Rm