The shells provide a number of wildcards that you can use to abbreviate filenames or refer to groups of files. For example, let's say you want to delete all filenames ending in .txt in the current directory (Section 1.16). You could delete these files one by one, but that would be boring if there were only 5 and very boring if there were 100. Instead, you can use a wildcarded name to say, "I want all files whose names end with .txt, regardless of what the first part is." The wildcard is the "regardless" part. Like a wildcard in a poker game, a wildcard in a filename can have any value.
The
wildcard you see most often is *
(an
asterisk), but we'll start with something simpler: ?
(a question mark). When it appears in a filename, the ?
matches any single character. For example,
letter?
refers to any filename that
begins with letter and has exactly one character after
that. This would include letterA,
letter1, as well as filenames with a nonprinting
character as their last letter, such as letter^C.
*.txt *mail *let
Sometimes you need to match a particular group of characters. For example, you
may want to list all filenames that begin with digits or all filenames that
begin with uppercase letters. Let's assume that you want to work with the files
program
.n
,
where n
is a single-digit number. Use the
filename:
program.[0123456789]
In other words, the wildcard [
character-list
]
matches any single character that appears
in the list. The character list can be any group of ASCII characters; however,
if they are consecutive (e.g., A-Z, a-z, 0-9, or 3-5, for that matter), you can
use a hyphen as shorthand for the range. For example, [a-zA-Z]
means any alphabetic English character.
There is
one exception to these wildcarding rules. Wildcards never match /
, which is both the name of the filesystem root (Section 1.14) and the character used to separate directory names in
a path (Section 1.16). The only way to match on this character is to
escape it using the backslash character ( \)
. However, you'll find it difficult to use the
forward slash within a filename anyway (the system will keep trying to use it as
a directory command).
If you are new to computers, you probably will catch on to Unix wildcarding
quickly. If you have used any other computer system, you have to watch out for
one important detail. Virtually all computer systems except for Unix consider a
period (.) a special character within a filename. Many operating
systems even require a filename to have a period in it. With these operating
systems, a *
does not match a period; you
have to say *.*
. Therefore, the equivalent of
rm *
does virtually nothing on some
operating systems. Under Unix, it is dangerous: it means "delete all the files
in the current directory, regardless of their name." You only want to give this
command when you really mean it.
But
here's the exception to the exception. The shells and the
ls command consider a . special if it is the first
character of a filename. This is often used to hide initialization files and
other files with which you aren't normally concerned; the ls command doesn't show these files unless you
ask (Section 8.9) for them. If a file's name begins with ., you always
have to type the . explicitly. For example, .*rc
matches all files whose names begin with . and end with
rc. This is a common convention for the names of Unix
initialization files.
Table 1-3 has a summary of common wildcards.
Wildcards can be used at any point or points within a path. Remember,
wildcards only match names that already exist. You can't use them to create new files (Section 28.3) — though many shells have
curly
braces ({}
) for doing that. Section 33.3 explains how wildcards are
handled, and Section 33.2 has more
about wildcards, including specialized wildcards in each of the
shells.