home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 1.15 Filenames Chapter 1
Introduction
Next: 1.17 Filename Extensions
 

1.16 Wildcards

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 in the current directory ( 1.21 ) ending in .txt . You could delete these files one by one, but that would be boring if there were only five and very boring if there were a hundred. 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 * (asterisk), but we'll start with something simpler: ? (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 one character after that. This would include letterA , letter1 , as well as filenames with a non-printing character as their last letter, like letter ^C.

The * wildcard matches any character or group of zero or more characters. For example, *.txt matches all files whose names end with .txt , *.c matches all files whose names end with .c (by convention, source code for programs in the C language), and so on.

The * and ? wildcards are sufficient for 90 percent of the situations that you will find. However, there are some situations that they can't handle. For example, you may want to list files whose names end with .txt , mail , or let . There's no way to do this with a single * ; it won't let you exclude the files you don't want. In this situation, use a separate * with each filename ending:

*.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 character.

There is one exception to these wildcarding rules. Wildcards never match / , which is both the name of the filesystem root ( 1.19 ) and the character used to separate directory names in a path ( 1.21 ) .

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 very 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 most operating systems. Under UNIX, it is very 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 that you aren't normally concerned with; the ls command doesn't show these files unless you ask ( 16.11 ) 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-1 has a summary of the different sorts of wildcards available.

Table 1.1: Shell
Wildcard Matches
? Any single character
* Any group of zero or more characters
[ab] Either a or b
[a-z] Any character between a and z, inclusive

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 ( 9.4 ) - though some shells have curly braces ( {} ) ( 9.5 , 15.3 ) for doing that. Article 1.18 has more about how wildcards are handled.

- ML


Previous: 1.15 Filenames UNIX Power Tools Next: 1.17 Filename Extensions
1.15 Filenames Book Index 1.17 Filename Extensions

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