So. I have to grep a string in a dir that contains files with same name except for hours:


  • filename_01_WORD-1.log
  • filename_02_WORD-1.log
  • filename_03_WORD-1.log
  • filename_04_WORD-1.log
  • filename_05_WORD-1.log
  • ...
  • filename_23_WORD-1.log
  • filename_24_WORD-1.log

I'm trying to grep a single word 'ABC' in just files of hours between 18 and 23.

I've tried with grep 'ABC' filename_[18-19-20-21-22-23]*WORD*.log but it is expanded as grep on files from 05 to 23 and I don't know why.

How can I achieve this?


Use brace expansion instead:

grep ABC filename_{18,19,20,21,22,23}_WORD*.log

or more compactly

grep ABC filename_{18..23}_WORD*.log

The way you've written it tells the shell to consider any file starting with filename_, followed by a single character matching one of the set 1, 8 to 1 (which doesn't match anything), 9 to 2 (likewise), 0 to 2, 1 to 2, 2 to 2 or 3 (so in the end, 0, 1, 2 or 3), followed by any characters, followed by WORD etc.

  • Ah... you beat me to it with a minute! Feb 3 '17 at 13:35
  • Thank you alot for all your effort on making me understand. Feb 3 '17 at 13:40

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.