When you have a number of files named in
series (for example, chap1.doc to
chap12.doc) or filenames with common characters
(such as aegis, aeon, and
aerie), you can use wildcards to specify many
files at once. These special characters are *
(asterisk), ?
(question mark), and [ ]
(square brackets). When used in a file or directory name
given as an argument on a command line, the characteristics detailed
in Table 4-1 are true.
Table 4-1. Shell wildcards
The following examples show the use of wildcards. The first command lists all the entries in a directory, and the rest use wildcards to list just some of the entries. The last one is a little tricky; it matches files whose names contain two (or more) a’s.
$ ls chap0.txt chap2.txt chap5.txt cold.txt chap1a.old.txt chap3.old.txt chap6.txt haha.txt chap1b.txt chap4.txt chap7.txt oldjunk $ls chap?.txt
chap0.txt chap4.txt chap6.txt chap2.txt chap5.txt chap7.txt $ls chap[3-7]*
chat3.old.txt chap4.txt chap5.txt chap6.txt chap7.txt $ls chap??.txt
chap1b.txt $ls *old*
chap1a.old.txt chap3.old.txt cold.txt oldjunk $ls *a*a*
chap1a.old.txt haha.txt
Wildcards are useful for more than listing files. Most Unix programs
accept more than one filename, and you can use wildcards to name
multiple files on the command line. For example, both the
cat
and less
programs display
files on the screen. cat
streams a
file’s contents until end of file, while
less
shows the file one screenfull at a time.
Let’s say you want to display files
chap3.old.txt and
chap1a.old.txt. Instead of specifying these
files individually, you could enter the command as:
$ less *.old.txt
This is equivalent to less chap1a.old.txt chap3.old.txt
.
Wildcards match directory names, too. You can use them anywhere in a
pathname—absolute or relative—though you still need to
separate directory levels with slashes (/
). For
example, let’s say you have subdirectories named
Jan, Feb,
Mar, and so on. Each has a file named
summary. You could read all the summary files by
typing less */summary
. That’s
almost equivalent to less Jan/summary Feb/summary
.
However, there’s one important difference when you
use less */summary
: the names will be
alphabetized, so Apr/summary would be first in
the list.
Get Learning Unix for Mac OS X Panther now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.