Introduction to UNIX for Web Developers Wild Cards There are several tools that are useful when submitting commands to the kernel via the shell. These tools help make tasks more efficient. One such tool is the wild card.

The wild cards that you will use most often include the asterisk (*), the question mark (?) and the brackets ([])

The asterisk is used to match any character zero or more times. For example, if we modify the "ls" command used in the last section, we can filter the output for only files starting with "s" using the command ls -l s*

The question mark on the other hand, is used to match any single character. Thus ls -l ???? will match any file with a name four characters long such as temp or "temp"

Finally, brackets are used to specify ranges or internal sets. Thus you can specify which characters from within a set you are looking for such as in the following case which matches any even number below 10: [02468]

Below is a table that shows some of the more common uses for wildcards with the "ls" command that you might use

Example Result
ls index* Matches any file beginning with the characters "index" including index.html, index.bak, index.cgi
ls *.html Matches any file ending in ".html" such as index.html or test.html. (Note however, that this would not match index.html.bak. To do that you would need *.html*)
ls a*.html matches all the HTML files starting with the letter "a"
ls ?? Matches any file with a filename of two characters such as "aa" or "bb"
ls ?.html Matches all HTML files with a single character name such as a.html or 1.html. 12.html would not match
ls [abcd]??.html Matches any file starting with "a", "b", "c" or "d" followed by any two characters, and followed by a .html ending.
ls part[0-9].doc Matches all .doc from part 1 through part 9 such as part4.doc.
ls part[A-Z].doc Matches all .doc from part A through #part Z such as "partM.doc". Notice that this is case sensitive so it would not match "partm.doc". To do that, you would need [a-z] or [a-zA-Z]

Previous | Next | Table of Contents