UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 14.14 Automatic Setup When You Enter/Exit a Directory Chapter 15Next: 15.2 Filename Wildcards in a Nutshell
 

15. Wildcards

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

15.1 File Naming Wildcards

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:

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.

- ML


Previous: 14.14 Automatic Setup When You Enter/Exit a Directory UNIX Power ToolsNext: 15.2 Filename Wildcards in a Nutshell
14.14 Automatic Setup When You Enter/Exit a Directory Book Index15.2 Filename Wildcards in a Nutshell

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System