Arguments to commands are frequently filenames. These might be the names of files the command should read, copy, or move. If you want to act on a number of files, you don't want to have to type every filename, especially when all the filenames have some pattern in common. For example, you might want to do something with a group of files whose names all start with Hello.
That's where wildcards come in. Wildcards (often called glob-patterns ) are special characters you can type in a command line to make a command apply to a group of files whose names match some patternfor example, all files ending in .jpg.
When the shell reads a command line, it expands any glob-patterns by replacing them with all the filenames that match. The shell then executes the command line, using the new list of arguments with the command.
To use a glob-pattern to match all filenames starting with Hello:
You can use more than one glob-pattern in a command line, such as rm *.jpg *.gif
This removes all the .jpg and .gif files from the current directory.
To use a glob-pattern to match only one single character:
You can combine the ? and * glob characters together. For example,
This would list files whose names begin with exactly two characters, followed by a period, followed by anything. (The period is matched literally.)
Sometimes you want to use a list of files that match a more specific pattern. For this you might use a more complex kind of pattern.
To match a range of characters:
would result in output similar to that shown in Figure 2.32 .
Figure 2.32. This shows the output when you use a glob-patternin this case [0-3] for a range of characters.
user-vc8f9gd:~ vanilla$ ls /var/log/system.log.[0-3].gz /var/log/system.log.0.gz /var/log/system.log.2.gz /var/log/system.log.1.gz /var/log/system.log.3.gz user-vc8f9gd:~ vanilla$
The [ and ] characters are used to create a glob-pattern called a character class . The resulting pattern matches any single character in the class. A range of characters can be indicated by using the hyphen, so that [0-3] is the same as .
Ranges may be alphabetical as well as numeric:
Unix filenames are case sensitive. You can match either case by including both in the character class:
You can create a character class that is quite arbitraryfor example, the glob-pattern
matches only Photo-A and Photo-D .
To negate a character class:
Patterns and rules similar to those described here are used in many different Unix tools, especially in a set of tools called regular expressions. See Chapter 4, "Useful Unix Utilities," for more on regular expressions.