Using Wild Cards to Locate Files in UNIX


Wild cards are a way of specifying one or more files using a pattern. When you type in a command the shell will search for the characters '*', '?', '{', '~' and '[' in each word of the command and its arguments. If one of the characters appears then the word is regarded as a pattern and is replaced with an alphabetically sorted list of file names that match the pattern. Patterns are matched in the context of the given word.  For example, if a legal character is given as part of the pattern, then the wild card attempts to match the file(s) given the character's position within the pattern.

Some of the most common patterns are:

• '*', which matches any number/sequence of characters (a string), including no characters.
• '?', which matches any single character.
• [...], which matches any one of the characters enclosed by the brackets (a pair of characters separated by a '-' matches any characters between the pair).
• {...}, like [...] except that it works with a comma separated list of strings.
• ~username, which matches the home directory of the given user (if the username is omitted, it matches your home directory).

Note: A more complete list of wildcards is available.

Unlike a Disk Operating System (DOS), where it is the command's responsibility to perform wild card expansion, UNIX lets the shell do the expansion. So the Command "command a?" does not pass "a?" to the command, rather, it passes however many files fit the description of "a?" to the command; DOS commands would simply get "a?" as the argument.

To make use of the wild cards above, the file must exist, or the shell will report No match. However the {...} pattern does not follow this convention; files that it attempts to match do not have to exist, the result is simply passed to the command

Mixing of the characters above are allowed (with the exception of the '~', which must occur at the beginning of the pattern). You can use any combination of the above in a single word or multiple words when using them. Another caveat is that "dot" files (filenames that begin with a '.') are not matched by wild cards unless the word explicitly begins with a '.'.