Wild and Crazy Wildcards
When you type a command, you may want to include the names of a bunch of files on the command line. UNIX makes the typing of multiple filenames somewhat easier (as though we should be grateful) by providing wildcards. Wildcards are the two special characters (still more of them to remember!) that have a special meaning in filenames:Wildcard | What It Means |
---|---|
? | Any single letter |
* | Anything at all |
Pick a letter, any letter
You can use one or more ? wildcards in a filename. Each ? stands for exactly one character — no more, no less. To list all your files that have two-letter names, for example, you can type this line:
ls ??
The command ls budget?? lists all filenames that start with budget and have two — and only two — characters after budget, like budget98 and budget99; the combination doesn’t match budget1 or budget.draft or Budget98 (because of the uppercase B).
Stars (***) in your eyes
The * wildcard stands for any number of characters. To list all your files that have names starting with a c, for example, type
ls c*
This specification matches files named customer.letter , c3 , and just plain c . The specification budget.* matches budget.2004 and budget.draft , but not draft.budget . The name *.draft matches budget.draft and window.draft , but not draft.horse or plain draft . By itself, the filename * matches everything (watch out when you let the asterisk go solo!).
Are kings or deuces wild?
Unlike some other kinds of operating systems (we don’t name any, although one system’s initials are DOS), UNIX handles the ? and * wildcards in the same way for every command. You don’t have to memorize which commands can handle wildcards and which ones cannot. In UNIX, they all can handle wildcards.Wildcards commonly are used with the ls , cp , rm , and mv commands. For example, to copy all the files from the current directory to the temp directory, you can type
cp * temp
Warning Look before you delete!
The combination of wildcards and the rm command is deadly. Use wildcards with care when you delete files. You should look first at the list of files you are deleting to make sure that it is what you had in mind. Before you type the following command, for example, to delete a bunch of files: rm *.03
type this line and look at the resulting list of files:
ls *.03
You may see in that list of .03 files something worth keeping that you forgot about.The most deadly typo of all is this one (do not type this line!): rm * .03
Notice the space between the * wildcard and the .03 . Although you may have thought that you were deleting all files ending with .03 , UNIX thinks that you have typed two filenames to delete:* This "filename" deletes all the files in the directory..03 This filename deletes a file named .03 (yes, filenames can start with a period). By the time UNIX tries to delete this (nonexistent) file, it has, of course, already deleted all the files in the directory!You end up with an empty directory and lots of missing files. Watch out when you use rm and * together!
Wildcards for Windows users
Although UNIX wildcards look just like Windows wildcards and they work in almost the same way, they have a few differences:
Because UNIX filenames don’t have to have the extensions that Windows filenames use, don’t use *.* to match all files in a directory. That trick matches only files that have a dot in their names. A simple * does the trick.
In Windows, you cannot put letters after the * wildcard — Windows ignores the letters following the asterisk. In Windows, d*mb is the same as d*, for example. It’s dumb, we know. The good news is that UNIX is not so dumb. In UNIX, d*mb works just the way you want it to.