Is there any option to enable confirmation for the rm -rf . We had an alias setup for rm=rm -i so whenever we delete a file it asks for confirmation but when -f flag is supplied it will not asks for confirmation.

So is there any option to ask confirmation for rm (Or rm -r) command with force flag that is for rm -f and rm -rf commands?


Sounds like you want to disable or enforce checking of the parameters. Take a look at safe-rm or create an alias with a function (like here).

  • Can you please help to create a function for it. I tried the following but its not working: function rm () { if [[ $@ == "-rf" ]]; then command rm -rfi else command rm "$@" fi } – Geo Jan 18 '17 at 15:00
  • @Geo It depends on what you want to do. Do you just want to "disable" rm -rf so that it won't work? That's not possible without a wrapper (like safe-rm). -f overrides -i, you can create functions or aliases as long as you like, but you can't change the mechanics of rm. – Lenniey Jan 18 '17 at 15:14
  • Ok thanks. I tried various options but its not working.. – Geo Jan 18 '17 at 15:26

rm -rf is a very powerful command, need to very conscious while executing it.

This may help you for enabling confirmation.


  • Thanks but alias will not work with -f flag. We need to setup alias for "rm -rf" but its not possible as there is a space. – Geo Jan 18 '17 at 10:25

Fixed asking confirmation on rm command with –f flag issue. Tested various deletion cases and is working.

You can add the following script in .bashrc file.

rm() {
     if [[ $* == -rf* ]]; then
           shift 1;
           command rm -rfi "$@" | more
     elif [[ ${@: -1} == -rf* ]]; then
           command rm "$@" -rfi | more
           command rm -i "$@"

Please make sure no alias for rm is set otherwise while executing the source .bashrc we will get error.

This works when we give –rf on first as well as on last like as follows and also it works for files (so no need of alias rm=rm-i)

[root@localhost ~]# mkdir test
[root@localhost ~]# rm -rf test
rm: remove directory ‘test’? 
[root@localhost ~]# rm test -rf
rm: remove directory ‘test’? 
[root@localhost ~]#

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.