Using Wildcard Characters Effectively


  
The Unix shell interprets a number of characters in a special way. These characters are known as wildcard characters. Usually these characters are used to describe filenames or directory names. The handling of files is simplified by using wildcard characters to match files that match particular patterns.

Wildcard It means
* An asterisk matches any number of characters in a filename, including none.
? The question mark matches any single character.
[ ] Brackets enclose a set of characters, any one of which may match a single character at that position.
- A hyphen used within [ ] denotes a range of characters.
~ A tilde at the beginning of a word expands to the name of your home directory. If you append another user's login name to the character, it refers to that user's home directory.


Here are some examples on how to use wildcard characters:

You type It will
$ cat c* Displays any file whose name begins with c including the file c, if it exists.
$ ls *.c Lists all files that have a .c file extension.
$ cp ../xyz? . Copies every file in one directory up that is four characters long and begins with xyz to the current working directory. (The names will remain the same.)
$ ls xyz[34567] Lists every file that begins with xyz and has a 3, 4, 5, 6, or 7 at the end.
$ ls xyz[3-7] Does exactly the same thing as the previous example.
$ ls ~ Lists your home directory.
$ ls ~/project Lists "project" directory under the home directory.

By using wildcard characters, you can pre-filter and zero-in the list of files and directories that you're really interested in. Then, common operations can thus be performed on a group of common files using a single command.