|
Page 1 of 4
Working with Wildcards
Shells support a rich set of wildcards to help work with files.
Awildcard is an expression that the shell uses to expand to a
number of file names&mdashthat is, to all files that match the
expression.
Wildcards are often called globs. The use of globs is called
globbing.
The * Wildcard
>
>
The main wildcard is a star, or asterisk (*), character. (Java
programmers sometimes call this a splat.) A star alone matches
anything and nothing, sort of Zen-like. Typically, you need to pair
a star with some other characters to form a more specific
expression. For example, *.txt matches all file names ending with
.txt, including all of the following:
.txt
a.txt
a_very_long_name.txt
A_FILE_NAME_WITH_UPPERCASE_LETTERS.txt
The * means that you don&rsquot care what letters there are
before the .txt.
Typically, an expression such as *.txt will match all text files.
You can refine the wildcard expression further. For example, a*.txt
matches all file names that start with a lowercase letter a and end
with .txt. Again, the * means you don&rsquot care about any
letters in between. Using the files from the previous list, a*.txt
would match just the following:
a.txt
a_very_long_name.txt
>
>
If you want files that start with an uppercase A or a lowercase a
and end with .txt, use the expression [Aa]*.txt. This expression
would match the following files:
a.txt
a_very_long_name.txt
A_FILE_NAME_WITH_UPPERCASE_LETTERS.txt
You can use the star more than once&mdashfor example, with the
expression a*v*.txt. This expression would match only one file in
the example list of files:
a_very_long_name.txt
On MS-DOS, each program has to support wildcards on its own. A few
DOS commands, such as DIR, support a limited number of wildcard
expressions. This is very different with Unix and Linux, as the
shell supports the wildcard expressions so each program
doesn&rsquot have to.
|