Up: wildcards


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 1 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 absolute), 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 recurse) 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


Footnotes

[1] Notice that earlier GNU tar versions used globbing for inclusion members, which contradicted to UNIX98 specification and was not documented. See Changes, for more information on this and other changes.