Contents:
File Naming Wildcards
Filename Wildcards in a Nutshell
Adding { } Operators to Korn (and Bourne) Shells
What if a Wildcard Doesn't Match?
Matching All "Dot Files" with Wildcards
Maybe You Shouldn't Use Wildcards in Pathnames
Getting a List of Matching Files with grep -l
Getting a List of Non-Matching Files with grep -c
nom: List Files that Don't Match a Wildcard
Wildcards that Match Only Directories
Wildcards (1.16) are the shell's way of abbreviating filenames. Just as in poker, where a wildcard is a special card that can match any card in the deck, filename wildcards are capable of matching letters, or groups of letters, in the alphabet. Rather than typing a long filename, or a long chain of filenames, a wildcard lets you provide parts of names, and then use some "wildcard characters" for the rest. For example, if you want to delete all files whose names end in .o, you can give the command:
%rm *.o
You don't have to list every filename.
I'm sure you already know that wildcards are useful in many situations. If not, they are summarized in article 15.2. Here are a few of my favorite wildcard applications:
If you remember part of a filename, but not the whole name, you can use wildcards to help you find it. If I have a file on genetics saved in a directory with several hundred other files, a command like:
%ls *gene*
will often find what I want. It's quicker and easier than find (17.1).
Wildcards are a natural when you want to work with groups of files. If I have a general purpose directory that's full of filenames ending in .c and .h, I can make new subdirectories and use wildcards to move the files easily:
%mkdir c h
%mv *.c c
%mv *.h h
Wildcards often help you to work with files with inconvenient characters in
their names. Let's say you have a file named abc
x
e
, where
x
is some unknown control character. You can delete or
rename that file by using the wildcarded name abc?e
. (When you
do this, be careful that your wildcard doesn't match more than you intend.)
Wildcards can appear in any component of a pathname. This can often be used to your advantage. For example, let's say that you have a directory named /work, split into subdirectories for a dozen different projects. For each project, you have a schedule, in a file called (obviously enough) schedule.txt. You can print all the schedules with the command:
%lpr /work/*/schedule.txt
BSD UNIX %lp /work/*/schedule.txt
System V UNIX
(However, you can occasionally run into problems (15.6).)
It's a common misconception, particularly among new users, that
application programs and utilities have something to do with
wildcards.
Given a command like grep ident *.c
, many users think
that grep handles the *
and looks to see which files have names
that end in .c. If you're at all familiar with UNIX's
workings, you'll realize that this is the wrong picture. The shell
interprets wildcards.
That is, the shell figures out which files have
names ending in .c, puts them in a list, puts that list on the
command line, and then hands that command line to grep. As it
processes the command line, the shell turns
grep ident *.c
into
grep ident file1.c file2.c ...
.
Since there are several shells, one might think (or fear!) that there should be several different sets of wildcards. Fortunately, there aren't. The C shell has made one significant extension (the curly brace operators (9.5)), and the Korn shell has made a few more, but the basic wildcards work the same for all shells.
-