Enable confirmation for rm command with force flag

0

2

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?

In .bashrc file tried setting up of alias as

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

but its not working. By referring this url : I tried to create a function as

function rm () 
{ if [[ $@ == "-rf" ]]; 
then command rm -rfi 
else command rm "$@" 
fi } 

but this also not working. Can anyone please help me to fix this.

Geo

Posted 2017-01-19T09:32:02.953

Reputation: 109

Answers

0

You can fix it by removing the -f option.

`-i` means interactive.
`-f` means force everything. That includes no confirmation.

Quoting part of man rm on my own system (No RedHat enterprise installation but close enough):

-f      Attempt to remove the files without prompting for confirmation,
        regardless of the file's permissions.  If the file does not
        exist, do not display a diagnostic message or modify the exit
        status to reflect an error.  The -f option overrides any previous
        -i options.

The last line answers your question.

Hennes

Posted 2017-01-19T09:32:02.953

Reputation: 60 739

0

You asked the same question and got your answer here. It won't work out of the box! You need a wrapper of some sort.

Lenniey

Posted 2017-01-19T09:32:02.953

Reputation: 615

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

Posted 2017-01-19T09:32:02.953

Reputation: 109