9

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?

1
  • I believe it is a bad idea to alias existing standard POSIX commands. Commented Aug 5, 2019 at 5:44

5 Answers 5

10

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.

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

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.

0
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.

4
  • But -i takes precedence over -f, so rm -rf -i will still ask for confirmation. Commented Feb 11, 2016 at 6:31
  • @Will The modifier that appears later takes precedent. so rm -f -i will prompt, but rm -i -f will not prompt. Commented Feb 11, 2016 at 9:03
  • Not on my system. Commented Feb 11, 2016 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. Commented Aug 25, 2016 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' 
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.