|
Filename SubstitutionThe AsteriskOne powerful feature of the Unix system that is actually handled by the shell is filename substitution. Let's say that your current directory has these files in it:
Suppose that you want to print their contents at the terminal. Well, you could take advantage of the fact that the cat command allows you to specify more than one filename at a time. When this is done, the contents of the files are displayed one after the other:
But you can also type in
and get the same results. The shell automatically substitutes the names of all the files in the current directory for the *. The same substitution occurs if you use * with the echo command:
Here the * is again replaced with the names of all the files contained in the current directory, and the echo command simply displays them at the terminal. Any place that * appears on the command line, the shell performs its substitution:
The * can also be used in combination with other characters to limit the filenames that are substituted. For example, let's say that in your current directory you have not only chapt1 through chapt4 but also files a, b, and c:
To display the contents of just the files beginning with chapt, you can type in
The chapt* matches any filename that begins with chapt. All such filenames matched are substituted on the command line. The * is not limited to the end of a filename; it can be used at the beginning or in the middle as well:
In the first echo, the *t1 specifies all filenames that end in the characters t1. In the second echo, the first * matches everything up to a t and the second everything after; thus, all filenames containing a t are printed. Because there are no files ending with x, no substitution occurs in the last case. Therefore, the echo command simply displays *x. Matching Single CharactersThe asterisk (*) matches zero or more characters, meaning that x* matches the file x as well as x1, x2, xabc, and so on. The question mark (?) matches exactly one character. So cat ? prints all files with one-character names, just as cat x? prints all files with two-character names beginning with x.
In the preceding example, the ?? matches two characters, and the * matches zero or more up to the end. The net effect is to match all filenames of two or more characters. Another way to match a single character is to give a list of the characters to use in the match inside square brackets [ ]. For example, [abc] matches one letter a, b, or c. It's similar to the ?, but it allows you to choose the characters that will be matched. The specification [0-9] matches the characters 0 through 9. The only restriction in specifying a range of characters is that the first character must be alphabetically less than the last character, so that [z-f] is not a valid range specification. By mixing and matching ranges and characters in the list, you can perform some complicated substitutions. For example, [a-np-z]* matches all files that start with the letters a through n or p through z (or more simply stated, any lowercase letter but o). If the first character following the [ is a !, the sense of the match is inverted. That is, any character is matched except those enclosed in the brackets. So
matches any character except a lowercase letter, and
matches any file that doesn't end with the lowercase letter o. Table 2.1 gives a few more examples of filename substitution.
|
|
No comments:
Post a Comment