Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On the command line, can I express easily in terms of shell globs that I would like to catch all files something-*.txt, except for something-*-foobar.txt? If I can, how?

NB: I know how to achieve the same with grep -v or other combinations of multiple commands. The question is solely about whether (and if so how) to achieve this with shell globs.

share|improve this question
up vote 8 down vote accepted

You can enable ksh-style extended globbing with:

shopt -s extglob

And then write something-!(*-foobar).txt

share|improve this answer
+1. You beat me to it. – Michael Kjörling Feb 18 '13 at 19:54
Thanks. Quick and concise. That seems to do the job nicely. Will it conflict with dotglob? – 0xC0000022L Feb 18 '13 at 19:58
@0xC0000022L extglob can be affected by dotglob if your extglob is very general, such as !(foo.txt). But if your dotglob is specific, as it is in this case, the dotglob won't matter. – kojiro Feb 19 '13 at 0:05
@kojiro: thanks, much appreciated. – 0xC0000022L Feb 19 '13 at 0:37

Your Answer


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.