Unix Power ToolsUnix Power ToolsSearch this book

33.2. Filename Wildcards in a Nutshell

This article summarizes the wildcards that are used for filename expansion (see Table 33-1). The shells use the same basic wildcards, though most shells have some extensions. Unless otherwise noted, assume that wildcards are valid for all shells.

Table 33-1. Filename wildcards

Wildcard

Shells

Description

*

All

Match zero or more characters. For example, a* matches the files a, ab, abc, abc.d, and so on. (zsh users: also see x# and x##, below.)

?

All

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

[12..a..z]

All

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

[a-z]

All

Match all characters between a and z, in a case-sensitive manner, based on the characters' value in the ASCII character set. For example, a[0-9] matches a0, a1, and so on, up to a9.

[!ab..z]

bash, ksh, zsh, newer sh

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

[^ab..z]

tcsh, zsh

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

<m-n>

zsh

Any number in the range m to n. If m is omitted, this matches numbers less than or equal to n. If n is omitted, it matches numbers greater than or equal to m. The pattern <-> matches all numbers.

{word1,word2...}

bash, csh, pdksh, zsh

Match word1, word2, etc. For example, a_{dog,cat,horse} matches the filenames a_dog, a_cat, and a_horse. These ( (Section 28.4) actually aren't filename-matching wildcards. They expand to all strings you specify, including filenames that don't exist yet, email addresses, and more. (If you want to match one or more of a group of filenames that already exist, see also the parenthesis operators ( ) below.)

?(x|y|z)

ksh, bash2

Match zero or one instance of any of the specified patterns. For example, w?(abc)w matches ww or wabcw. Also, ?(foo|bar) matches only foo, bar, and the empty string. In bash2, this works only if you've set the extglob option using shopt.

*(x|y|z)

ksh, bash2

Match zero or more instances of any of the specified patterns. For example, w*(abc)w matches ww, wabcw, wabcabcw, etc. Also, *(foo|bar) matches foo, bar, foobarfoo, etc., as well as the empty string. In bash2, this works only if you've set the extglob option using shopt.

+(x|y|z)

ksh, bash2

Match one or more instances of any of the specified patterns. For example, w+(abc)w matches wabcw, wabcabcw, etc. Also, +(foo|bar) matches foo, bar, foobarfoo, etc. In bash2, this works only if you've set the extglob option using shopt.

@(x|y|z)

ksh, bash2

Match exactly one of any of the specified patterns. For example, @(foo|bar) matches foo or bar. (See also {word1,word2...}.) In bash2, this works only if you've set the extglob option using shopt.

!(x|y|z)

ksh, bash2

Match anything that doesn't contain any of the specified patterns. For example, w!(abc)w doesn't match wabcw or wabcabcw, but it does match practically anything else that begins or ends with w. Also, !(foo|bar) matches all strings except foo and bar. In bash2, this works only if you've set the extglob option using shopt. (For other shells, see nom (Section 33.8).)

^pat

tcsh, zsh

Match any name that doesn't match pat. In zsh, this only works if you've set the EXTENDED_GLOB option. In tcsh, the pat must include at least one of the wildcards *, ? and [ ]. So, to match all except a single name in tcsh, here's a trick: put brackets around one character. For instance, you can match all except abc with ^ab[c]. (For other shells, see nom (Section 33.8).)

(x|y)

zsh

Match either x or y. The vertical bar (|) must be used inside parentheses.

**

zsh

Search recursively.

***

zsh

Search recursively, following symbolic links to directories.

x#

zsh

Matches zero or more occurrences of the pattern x (like the regular expresssion (Section 32.2) x*). The pattern can have parentheses ( ) around it. You must have set the EXTENDED_GLOB option.

x##

zsh

Matches one or more occurrences of the pattern x (like the regular expresssion (Section 32.15) x+). The pattern can have parentheses ( ) around it. You must have set the EXTENDED_GLOB option.

Note that wildcards do not match files whose names begin with a dot (.), like .cshrc. This prevents you from deleting (or otherwise mucking around with) these files by accident. The usual way to match those files is to 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 ... If you're constantly needing to match dot-files, though, you can set the bash variable glob_dot_filenames and the zsh option GLOB_DOTS to include dot-files' names in those shells' wildcard expansion.

You can prevent wildcard expansion by quoting ( Section 27.12, Section 27.13), of course. In the C shells, you can stop all wildcard expansion (which is also called globbing, by the way) without quoting if you set the noglob shell variable. In bash, ksh, and zsh, set the noglob option.

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 we 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.

--JP, ML, and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.