I have files that fall into two extension categories (.out and .error). Is there a single command that can delete them all at once?

I tried rm -f *.out || *.error but it did not work. Thanks

(i saw the linked post too but am not sure how to deal with multiple extensions still)

Edit: non recursive case

  • shopt -s globstar; rm **/*.{out,error}, or find . -type f \( -iname '*.out' -o -iname '*.error' \) -delete – muru Aug 16 '17 at 3:45
  • your command removes all .out files, and only if that [||] failed (none-found or permissions errors) process the names of all files ending in .error as commands - yuk! your description doesn't match what your command. a rm *.out *.error would have done what you want (-f if you need it, assuming current directory only) – guiverc Aug 16 '17 at 3:48
  • The answer is simple: rm *.out *error. Note that muru's commands above are recursive -- i.e. they will delete files in subdirectories too. – wjandrea Aug 16 '17 at 4:14
  • @muru See OP's edit. The question is not quite the same. OP is asking about multiple extensions, non-recursive deletion. The linked question is about one extension, recursive deletion. – wjandrea Aug 16 '17 at 4:17
  • @wjandrea are you sure it's non-recursive? – muru Aug 16 '17 at 4:21

The || is not needed there1, rm will act on all operands, so:

rm *.error *.out

Or, using bash's brace expansion (useful if you have a long list):

rm *.{error,out}

1Not only is || is not needed, it also changes the command structure. || is bash's OR for commands. So, if you had files a.error, b.error, and a.out, b,out, bash would execute:

rm a.out b.out

And if that fails, then execute a.error with b.error as an argument. It won't be passing a.error or b.error to a second run of rm.

| improve this answer | |

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.