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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 15.1 File Naming Wildcards Chapter 15
Wildcards
Next: 15.3 Adding { } Operators to Korn (and Bourne) Shells
 

15.2 Filename Wildcards in a Nutshell

This section summarizes the wildcards that are used for filename expansion. The shells use the same basic wildcards, though csh , tcsh , ksh , and bash have some extensions. Unless otherwise noted, assume that wildcards are valid for all shells.

*

Match zero or more characters. For example, a* matches the files a , ab , abc , abc.d , and so on.

?

Match exactly one character. For example, a? matches aa , ab , ac , etc.

[12..a..z]

Match any character listed in the brackets. For example, a[ab] matches aa or ab .

[a-z]

Match all characters between a and z. For example, a[0-9] matches a0 , a1 , and so on, up to a9 .

[!ab..z]

Match any character that does not appear within the brackets. For example, a[!0-9] doesn't match a0 , but does match aa . bash , Korn, and newer Bourne shells only.

[^ab..z]

Match any character that does not appear within the brackets. For example, a[^0-9] doesn't match a0 , but does match aa . tcsh only.

{word1,word2...}

Match word1 , word2 , etc. E.g., a_{dog,cat,horse} matches the filenames a_dog , a_cat , and a_horse . bash and C shells only. These (9.5 ) actually aren't filename-matching wildcards. They expand any string, including filenames that don't exist yet, email addresses, and more.

?(abc)

Match zero or one instance of abc . For example, x?(abc)x matches xx or xabcx . Korn shell only.

*(abc)

Match zero or more instances of abc . For example, x*(abc)x matches xx , xabcx , xabcabcx , etc. Korn shell only.

+(abc)

Match one or more instances of abc . For example, x+(abc)x matches xabcx , xabcabcx , etc. Korn shell only.

!(abc)

Match anything that doesn't contain abc . For example, x!(abc)x doesn't match xabcx or xabcabcx , but does match practically anything else that begins or ends with x . Korn shell only.

^ pat

Match any name that doesn't match pat . pat must include at least one of the wildcards * , ? and [] . To match all except a single name, here's a trick: put brackets around one character. For instance, you can match all except abc with ^ab[c] . tcsh only. (For other shells, see nom (15.9 ) .)

Note: wildcards do not match files whose names begin with a dot (. ), like .cshrc . [1] This prevents you from deleting (or otherwise mucking around with) these files by accident. To match those files, type the dot literally. For example, .[a-z]* matches anything whose name starts with a dot and a lowercase letter. Watch out for plain .* , though; it matches the directory entries . and .. (see article 15.5 for suggestions on solving that problem).

[1] Setting the bash variable glob_dot_filenames includes these names in wildcard expansion.

And a final note: many operating systems (VAX/VMS and DOS included) consider a file's name and extension to be different entities; therefore, you can't use a single wildcard to match both. What do I mean? Consider the file abc.def . Under DOS or VMS, to match this filename you'd need the wildcard expression *.* . The first * matches the name (the part before the period), and the second matches the extension (the part after the period). Although UNIX uses extensions, they aren't considered a separate part of the filename, so a single * will match the entire name.

- ML , JP


Previous: 15.1 File Naming Wildcards UNIX Power Tools Next: 15.3 Adding { } Operators to Korn (and Bourne) Shells
15.1 File Naming Wildcards Book Index 15.3 Adding { } Operators to Korn (and Bourne) Shells

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