When I use the command
rm -rf, I want to make sure a prompt always appears before deleting a file, so I tried adding this to
alias 'rm -rf'='rm -rfi'
But it doesn't work. How can I fix this?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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"
~/.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
[ -f ~/.bashrc ] && . ~/.bashrc
~/.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.
rm -rfi would give you the prompt, however
rm -i -rf would not.
Your alias make your commands to the latter one.
In addition to valid answers, I would advise you to separate your aliases into
~/.bash_aliases, and source it simpy in the end of
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
As has been already said, your alias is twisted into mutually exclusive options:
‘-f’ ‘--force’ Ignore nonexistent files and missing operands, and never prompt the user. Ignore any previous ‘--interactive’ (‘-i’) option.
^ taken from
Thus, the following would work:
alias rm='\rm -i'