2

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 03: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 03: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 04: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 04:17
  • @wjandrea are you sure it's non-recursive? – muru Aug 16 '17 at 04:21
  • @muru yeah, op didn't mention anything about recursion. – wjandrea Aug 16 '17 at 04:23
  • @wjandrea but they did say all files. – muru Aug 16 '17 at 04:24
  • Thanks wjandrea and @muru. I don't know why that didn't come to me (considering this is default in a lot of canonical programs like mkdir [dir1, ...] and cat [file1,..]) – information_interchange Aug 16 '17 at 05:00

1 Answers1

7

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.

muru
  • 197,895
  • 55
  • 485
  • 740