< Previous | Contents | Next >
Wildcards
Before we begin using our commands, we need to talk about a shell feature that makes these commands so powerful. Since the shell uses filenames so much, it provides special characters to help you rapidly specify groups of filenames. These special characters are
called wildcards. Using wildcards (which is also known as globbing) allows you to select filenames based on patterns of characters. The table below lists the wildcards and what they select:
Table 4-1: Wildcards
Wildcard Meaning
Wildcard Meaning
* Matches any characters
? Matches any single character
[characters] Matches any character that is a member of the set characters
[!characters] Matches any character that is not a member of the set
characters
[[:class:]] Matches any character that is a member of the specified
class
Table 4-2 lists the most commonly used character classes:
Table 4-2: Commonly Used Character Classes
Character Class Meaning
Character Class Meaning
[:alnum:] Matches any alphanumeric character [:alpha:] Matches any alphabetic character [:digit:] Matches any numeral
[:lower:] Matches any lowercase letter
[:upper:] Matches any uppercase letter
Using wildcards makes it possible to construct very sophisticated selection criteria for filenames. Here are some examples of patterns and what they match:
Table 4-3: Wildcard Examples
Pattern Matches
Pattern Matches
* All files
g* Any file beginning with “g”
b*.txt Any file beginning with “b” followed by any characters and ending with “.txt”
Wildcards
Data??? Any file beginning with “Data” followed by exactly three characters
[abc]* Any file beginning with either an “a”, a “b”, or a “c”
BACKUP.[0-9][0-9][0-9] Any file beginning with “BACKUP.”
followed by exactly three numerals
[[:upper:]]* Any file beginning with an uppercase letter
[![:digit:]]* Any file not beginning with a numeral
*[[:lower:]123] Any file ending with a lowercase letter or the numerals “1”, “2”, or “3”
Wildcards can be used with any command that accepts filenames as arguments, but we’ll talk more about that in Chapter 7.
Character Ranges
If you are coming from another Unix-like environment or have been reading some other books on this subject, you may have encountered the [A-Z] or the [a-z] character range notations. These are traditional Unix notations and worked in older versions of Linux as well. They can still work, but you have to be very careful with them because they will not produce the expected results unless properly configured. For now, you should avoid using them and use character classes instead.
Wildcards Work In The GUI Too
Wildcards are especially valuable not only because they are used so frequently on the command line, but are also supported by some graphical file managers.
● In Nautilus (the file manager for GNOME), you can select files using the Edit/Select Pattern menu item. Just enter a file selection pattern with wild- cards and the files in the currently viewed directory will be highlighted for se- lection.
● In some versions of Dolphin and Konqueror (the file managers for KDE), you can enter wildcards directly on the location bar. For example, if you want to see all the files starting with a lowercase “u” in the /usr/bin directory, enter “/usr/bin/u*” in the location bar and it will display the result.
Many ideas originally found in the command line interface make their way into the graphical interface, too. It is one of the many things that make the Linux desk- top so powerful.