0

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?

Geo
  • 575
  • 3
  • 9
  • 23

3 Answers3

1

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

Lenniey
  • 5,220
  • 2
  • 18
  • 29
  • 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
0

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

This may help you for enabling confirmation.

http://www.howtogeek.com/183690/enable-the-confirmation-when-deleting-files-and-folders-using-the-rm-command-in-ubuntu/

Caterpillar
  • 1,132
  • 2
  • 23
  • 47
  • 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
0

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
    else
           command rm -i "$@"
    fi
}

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 ~]#
Geo
  • 575
  • 3
  • 9
  • 23