Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I just created alias rm="rm -I" to prevent from accidental rm command.

-I     prompt  once before removing more than three files, or when removing recursively.
       Less intrusive than -i, while still giving protection against most mistakes

But it doesn't work. I don't get any prompt when I use rm -rf

share|improve this question

2 Answers 2

up vote 9 down vote accepted

If you read the help again you'll see it says previous --interactive. This means you can add an -I after all the other arguments (like at the end of the line) and it'll be honoured again!

To do that, you need a function instead of an alias, but just stick this somewhere in your .bashrc (et al) and you're golden:

function rm { /bin/rm "$@" -I; }

A perhaps safer method than even this is using trash-cli's trash-put command. It will put things in your account's trash directory that can be reviewed/restored/emptied normally. It also has dummy -r, -f and, -i arguments so it makes a perfect alias possible:

alias rm="trash-put"
share|improve this answer
2  
Sounds fair, but better is not playing with -rf ;) –  Braiam Dec 2 '13 at 19:37
4  
as long as one of those arguments isn't --... –  Drake Clarris Dec 2 '13 at 21:07
    
@DrakeClarris Yeah I've been thinking about that. This is a bash function so we could pick apart the "$@", searching for ` --` and replacing it with -I --. You'd also need to make sure that was unquoted. It feels like this would be pretty tedious to work around. –  Oli Dec 2 '13 at 21:39

The problem here is that -f makes rm ignore any interactive command:

`-f'
`--force'
   Ignore nonexistent files and missing operands, and never prompt
   the user.  Ignore any previous `--interactive' (`-i') option.

Source: info coreutils 'rm invocation'

share|improve this answer
    
So what option do I have to save myself from rm -rf? –  Sourabh Dec 2 '13 at 19:09
7  
@Sourabh just don't type it... –  Braiam Dec 2 '13 at 19:12
2  
This has been beaten to death on other threads, but it's still worth mentioning. It's not a good idea to create aliases or scripts that modify the behavior of common commands. 1) If you get used to the new behaviour and then use another system, it will not work as you expect. 2) If anyone else ever uses your system (e.g. to help you solve a problem), then those commands will not work as they expect them to. It's fine to have aliases and custom scripts, that's what Linux is about. Just don't name them the same as preexisting commands. –  Joe Dec 4 '13 at 21:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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