3

I want to create a script that whenever there are more than three files being removed, it invokes rm -i by default. I know it will go in initialization files but can't seem to get it working.

So far:

if [$file -ge 3]; then
  rm -i 
  exit 0 
else
  rm
exit 1
jasonwryan
  • 73,126

3 Answers3

15

The function given in @Tim's answer will work on any system running bash. However, GNU rm already has an option for what you need (from man rm):

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

So, if you have GNU rm (which you should if you're running Linux) all you need is to add this line to your /.bashrc:

alias rm='rm -I'

This will prompt before deleting more than 3 files, but it will not ask for confirmation for every file, if you confirm the action, all files will be deleted.

terdon
  • 242,166
8

This works for me, add these lines to your ~/.bashrc:

rm() {
   if [ "$#" -ge 3 ]; then
        command rm -i "$@"
   else
        command rm "$@"
   fi
}

This will make an rm function which will be called instead of /bin/rm every time you execute rm.

"$#" expands to the number of arguments passed.

Tim
  • 6,141
  • i put that in bashrc, and started the terminal, it gave me an error that [0 Command not found rm: missing operand – user2805361 Sep 23 '13 at 21:00
  • 2
    @user2805361 Did you copy that code exactly? The error is strange (did you copy-paste it?) but it looks like you wrote [$# instead of [ $# (missing space). Also this code is designed for use in a script or in a function. – Gilles 'SO- stop being evil' Sep 23 '13 at 21:12
  • I tried the edit still the same error when i start a new terminal. and yes code is exact, can i use the same code in .bashrc though? – user2805361 Sep 23 '13 at 21:13
  • @user2805361 As Gilles said, if you want to put this in your .bashrc, you should make it into a function. Try wrapping this exact code in rm () { ... }. – Joseph R. Sep 23 '13 at 21:15
  • if [ $# -ge 3 ]; then (rm -i "${@}") else (rm "${@}") fi \Whats wrong here? without the curly braces it still gives an error – user2805361 Sep 23 '13 at 21:18
  • 2
    @Tim Your answer works fine with a glob pattern: the shell has to expand the glob before actually calling the command/function/script,... – Joseph R. Sep 23 '13 at 21:23
  • after adding this to bashrc, when i start the terminal and remove three files, it should prompt me but it isnt doing that – user2805361 Sep 23 '13 at 21:25
  • if [ $# -ge 3 ]; then 'rm -i' "$@" \ else 'rm' "$@" fi i added that backslash and terminal didnd give error but still when deleting it dosent prompt – user2805361 Sep 23 '13 at 21:31
  • @user2805361 I edited the answer, if you want it in your .bashrc, you'll need something like that. Try the updated answer and let us know. – terdon Sep 23 '13 at 21:42
  • Ahh thanks @JosephR. that is the part I wasn't sure about! – Tim Sep 24 '13 at 12:34
1

Here is a concise still portable way to do it:

rm() {
  command rm $([ ${#} -gt 3 ] && echo \-i) "$@"
}

I would however recommend to avoid changing rm semantics, see this reply for details.

jlliagre
  • 61,204