start page | rating of books | rating of authors | reviews | copyrights

Learning the Unix Operating System

Learning the Unix Operating SystemSearch this book
Previous: 4.2 File and Directory NamesChapter 4
File Management
Next: 4.4 Managing Your Files
 

4.3 File and Directory Wildcards

When you have a number of files named in series (for example, chap1 to chap12) or filenames with common characters (like aegis, aeon, and aerie), you can use wildcards (also called metacharacters) to specify many files at once. These special characters are * (asterisk), ? (question mark), and [ ] (square brackets). When used in a filename given as an argument to a command:

*

An asterisk is replaced by any number of characters in a filename. For example, ae* would match aegis, aerie, aeon, etc. if those files were in the same directory. You can use this to save typing for a single filename (for example, al* for alphabet.txt) or to name many files at once (as in ae*).

?

A question mark is replaced by any single character (so h?p matches hop and hip, but not help).

[ ]

Square brackets can surround a choice of characters you'd like to match. Any one of the characters between the brackets will be matched. For example, [Cc]hapter would match either Chapter or chapter, but [ch]apter would match either capter or hapter. Use a hyphen (-) to separate a range of consecutive characters. For example, chap[13] would match chap1, chap2, or chap3.

The examples below demonstrate 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
chap10       chap2        chap5     cold
chap1a.old   chap3.old    chap6     haha
chap1b       chap4        chap7     oldjunk
% ls chap?
chap2    chap5    chap7
chap4    chap6
% ls chap[5-8]
chap5    chap6    chap7
% ls chap??
chap10   chap1b
% ls *old
chap1a.old   chap3.old    cold
% ls *a*a*
chap1a.old   haha

Wildcards are useful for more than listing files. Most UNIX commands accept more than one filename, and you can use wildcards to put multiple files on the command line. For example, the command more is used to display a file on the screen. Let's say you want to display files chap3.old and chap1a.old. Instead of specifying these files individually, you could enter the command as:

% more *.old

This is equivalent to "more chap1a.old chap3.old".

Wildcards match directory names, too. 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 "more */summary". That's almost equivalent to "more Jan/summary Feb/summary ..." but there's one important difference: The names will be alphabetized, so Apr/summary would be first in the list.


Previous: 4.2 File and Directory NamesLearning the Unix Operating SystemNext: 4.4 Managing Your Files
4.2 File and Directory NamesBook Index4.4 Managing Your Files

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