Extended globbing

Creative use of globbing syntax can do a great deal of advanced pattern matching, but in some cases we may need something more advanced, perhaps approaching regular expressions in terms of complexity. Bash's extglob feature allows us to do this. With this shopt option enabled, we can use some additional glob forms:

  • ?(pattern): Match up to one occurrence of the pattern
  • *(pattern): Match any number of occurrences of the pattern
  • +(pattern): Match at least one occurrence of the pattern
  • @(pattern): Match exactly one occurrence of the pattern
  • !(pattern): Match everything except the pattern

For example, /var/log/!(*.gz) matches all files in /var/log that don't end in .gz.

Some of these are useful only when using multiple patterns ...

Get Bash Quick Start Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.