Next: quoting styles, Previous: exclude, Up: Choosing [Contents][Index]

### 6.5 Wildcards Patterns and Matching

Globbing is the operation by which wildcard characters, ‘`*`’ or ‘`?`’ for example, are replaced and expanded into all existing files matching the given pattern. GNU `tar` can use wildcard patterns for matching (or globbing) archive members when extracting from or listing an archive. Wildcard patterns are also used for verifying volume labels of `tar` archives. This section has the purpose of explaining wildcard syntax for `tar`.

A pattern should be written according to shell syntax, using wildcard characters to effect globbing. Most characters in the pattern stand for themselves in the matched string, and case is significant: ‘`a`’ will match only ‘`a`’, and not ‘`A`’. The character ‘`?`’ in the pattern matches any single character in the matched string. The character ‘`*`’ in the pattern matches zero, one, or more single characters in the matched string. The character ‘`\`’ says to take the following character of the pattern literally; it is useful when one needs to match the ‘`?`’, ‘`*`’, ‘`[`’ or ‘`\`’ characters, themselves.

The character ‘`[`’, up to the matching ‘`]`’, introduces a character class. A character class is a list of acceptable characters for the next single character of the matched string. For example, ‘`[abcde]`’ would match any of the first five letters of the alphabet. Note that within a character class, all of the “special characters” listed above other than ‘`\`’ lose their special meaning; for example, ‘`[-\\[*?]]`’ would match any of the characters, ‘`-`’, ‘`\`’, ‘`[`’, ‘`*`’, ‘`?`’, or ‘`]`’. (Due to parsing constraints, the characters ‘`-`’ and ‘`]`’ must either come first or last in a character class.)

If the first character of the class after the opening ‘`[`’ is ‘`!`’ or ‘`^`’, then the meaning of the class is reversed. Rather than listing character to match, it lists those characters which are forbidden as the next single character of the matched string.

Other characters of the class stand for themselves. The special construction ‘`[a-e]`’, using an hyphen between two letters, is meant to represent all characters between a and e, inclusive.

Periods (‘`.`’) or forward slashes (‘`/`’) are not considered special for wildcard matches. However, if a pattern completely matches a directory prefix of a matched string, then it matches the full matched string: thus, excluding a directory also excludes all the files beneath it.

Next: quoting styles, Previous: exclude, Up: Choosing [Contents][Index]