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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.