Regular expressions

Wildcard characters

Wildcards are used to match filenames. In addition to literal filenames, the shells recognize the following simple regular expressions:

Matches any string of characters, including the null string (nothing). Thus, foo* matches ``foot'', ``football'' and ``foo''.

For example, ls * will match all the files in the current directory and its subdirectories. (The shell expands the ``*'' pattern; the ls command displays the results.)

ls g* will match all files beginning with the letter ``g''. In this case, the shell interprets the regular expression as meaning ``any string of zero or more characters following a letter ``g''.

(Note that by convention, ``dot'' files such as .profile are excluded from such listings. In order to display these, it is necessary to use the ls command's -a option.)

Matches any single character. Thus, foo?s will match ``foods'' or ``fools'' but not ``footballs''. To match all files with a name comprising four characters type ls ????.

[ ... ]
Matches one of the characters enclosed in brackets. (This is similar to ``?'', but restricted to the specified set of characters.) For example, the pattern [Aa] matches only the letters ``A'' and ``a''. ls [Aa]ardvark will match files called ``aardvark'' and ``Aardvark''. This is a useful construction in the light of the system's case-sensitivity.

A pair of characters separated by a ``-'' is taken to be a range. For example, [A-Z] is equivalent to [ABCDEFGHIJKLMNOPQRSTUVWXYZ] and ls [a-m]* will match all the files beginning with the letters ``a'' to ``m''. If the first character after the opening bracket is an exclamation mark (!), then any character not enclosed in the brackets is matched. For example, [!0-9] will match any character except a digit.

ls [!a-m]* will match all the files that do not begin with letters ``a'' through ``m''.

Because the hyphen has a special meaning in a set, you can match a literal hyphen within a set only by placing it at the beginning or the end of the set. For example, ls [abcde-]* will match files beginning with a--e or -.

For more information about wildcard regular expressions, see regexp(M).
Next topic: Editor regular expressions
Previous topic: Metacharacters in regular expressions

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003