5

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 ~/.bashrc

alias 'rm -rf'='rm -rfi'

But it doesn't work. How can I fix this?

  • I believe it is a bad idea to alias existing standard POSIX commands. – Basile Starynkevitch Aug 5 at 5:44
4

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.

  • Aliasing rm is a horrible idea for reasons stated here: apple.stackexchange.com/questions/17622/… – rouble Feb 12 '16 at 5:40
  • @rouble Search for package trash-cli that provides a /bin/trash. – user79743 Feb 12 '16 at 7:42
  • but there is no trash-cli on Centos7 @BinaryZebra – LawrenceLi Feb 12 '16 at 18:48
  • @LawrenceLi it was an attempt to make sense of what msw wrote. – user79743 Feb 12 '16 at 19:05
5

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.

2

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.

  • But -i takes precedence over -f, so rm -rf -i will still ask for confirmation. – Will Feb 11 '16 at 6:31
  • @Will The modifier that appears later takes precedent. so rm -f -i will prompt, but rm -i -f will not prompt. – RealSkeptic Feb 11 '16 at 9:03
  • Not on my system. – Will Feb 11 '16 at 12:28
  • 1
    @Will: Using rm on OS X El Capitan 10.11.6: "The -f option overrides any previous -i options." Seeing as there are different implementations of this program, I'd advise against aliasing rm at all. – damd Aug 25 '16 at 13:37
1

rm -rfi would give you the prompt, however rm -i -rf would not. Your alias make your commands to the latter one.

0

In addition to valid answers, I would advise you to separate your aliases into ~/.bash_aliases, and source it simpy in the end of ~/.bashrc

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 info rm.

Thus, the following would work:

alias rm='\rm -i'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.