Penguin

A File Glob is a way to match files.

  • "*" matches any character.
  • "?" matches any character exactly once.
  • "[a-z]" matches any character in the range a-z.

Some examples:

*.* matches all files/dirs that contain a "." in them.

a* matches all files/dirs that begin with a

[a-z]* matches all files/dirs that begin with a lowercase letter

*.gz matches all files/dirs that end with .gz

[^A-C]* matches all files/dirs that do not begin with A or B or C.

Note, * and ? __don't__ match / or a . at the beginning of a filename eg: * doesn't match:

1) .foo
2) baz/nargle

but does match:

1) foo.txt
2) baz/

extglob

This is a shell option that can be enabled by doing

  • shopt -s extglob

Here are the extra pattern matching operators we can now use thanks to extglob:

              ?(pattern-list)
                     Matches zero or one occurrence of the given patterns
              *(pattern-list)
                     Matches zero or more occurrences of the given patterns
              +(pattern-list)
                     Matches one or more occurrences of the given patterns
              @(pattern-list)
                     Matches one of the given patterns
              !(pattern-list)
                     Matches anything except one of the given patterns

pattern-list is just a bunch of globs separated by the | character.

extglob Examples

1) cp !(*.[Jj][Pp][Gg]|*.[Bb][Mm][Pp]) /dest/dir will copy every file to ''/dest/dir'' that is not a jpg or bmp file.

2) ls @(*foo*|*bar*|*car*) will list every file/directory that has either foo, bar or car in its name.

3) mv file+([0-9]) /dest/dir/ will move every file/directory that has a prefix of ''file'' followed by at least one number.

See glob(7) for more advanced uses.

Contrast RegularExpression.