[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

Controlling Pattern-Matching

For the purposes of this section, we call exclusion members all member names obtained while processing ‘--exclude’ and ‘--exclude-from’ options, and inclusion members those member names that were given in the command line or read from the file specified with ‘--files-from’ option.

These two pairs of member lists are used in the following operations: ‘--diff’, ‘--extract’, ‘--list’, ‘--update’.

There are no inclusion members in create mode (‘--create’ and ‘--append’), since in this mode the names obtained from the command line refer to files, not archive members.

By default, inclusion members are compared with archive members literally (15) and exclusion members are treated as globbing patterns. For example:

 
$ tar tf foo.tar
a.c
b.c
a.txt
[remarks]
# Member names are used verbatim:
$ tar -xf foo.tar -v '[remarks]'
[remarks]
# Exclude member names are globbed:
$ tar -xf foo.tar -v --exclude '*.c'
a.txt
[remarks]

This behavior can be altered by using the following options:

--wildcards

Treat all member names as wildcards.

--no-wildcards

Treat all member names as literal strings.

Thus, to extract files whose names end in ‘.c’, you can use:

 
$ tar -xf foo.tar -v --wildcards '*.c'
a.c
b.c

Notice quoting of the pattern to prevent the shell from interpreting it.

The effect of ‘--wildcards’ option is canceled by ‘--no-wildcards’. This can be used to pass part of the command line arguments verbatim and other part as globbing patterns. For example, the following invocation:

 
$ tar -xf foo.tar --wildcards '*.txt' --no-wildcards '[remarks]'

instructs tar to extract from ‘foo.tar’ all files whose names end in ‘.txt’ and the file named ‘[remarks]’.

Normally, a pattern matches a name if an initial subsequence of the name's components matches the pattern, where ‘*’, ‘?’, and ‘[...]’ are the usual shell wildcards, ‘\’ escapes wildcards, and wildcards can match ‘/’.

Other than optionally stripping leading ‘/’ from names (see section Absolute File Names), patterns and names are used as-is. For example, trailing ‘/’ is not trimmed from a user-specified name before deciding whether to exclude it.

However, this matching procedure can be altered by the options listed below. These options accumulate. For example:

 
--ignore-case --exclude='makefile' --no-ignore-case ---exclude='readme'

ignores case when excluding ‘makefile’, but not when excluding ‘readme’.

--anchored
--no-anchored

If anchored, a pattern must match an initial subsequence of the name's components. Otherwise, the pattern can match any subsequence. Default is ‘--no-anchored’ for exclusion members and ‘--anchored’ inclusion members.

--ignore-case
--no-ignore-case

When ignoring case, upper-case patterns match lower-case names and vice versa. When not ignoring case (the default), matching is case-sensitive.

--wildcards-match-slash
--no-wildcards-match-slash

When wildcards match slash (the default for exclusion members), a wildcard like ‘*’ in the pattern can match a ‘/’ in the name. Otherwise, ‘/’ is matched only by ‘/’.

The ‘--recursion’ and ‘--no-recursion’ options (see section Descending into Directories) also affect how member patterns are interpreted. If recursion is in effect, a pattern matches a name if it matches any of the name's parent directories.

The following table summarizes pattern-matching default values:

Members

Default settings

Inclusion

--no-wildcards --anchored --no-wildcards-match-slash

Exclusion

--wildcards --no-anchored --wildcards-match-slash


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Sergey Poznyakoff on October, 20 2013 using texi2html 1.78.