|
|
The shell recognizes three of the special characters listed in ``Characters with special meanings in the shell language'' --the asterisk (*), the question mark (?), and the set of brackets ([ ])--as symbols for patterns that are parts of filenames. By substituting one or more of these characters for the name (or partial name) of an existing file (or group of files), you can reduce the amount of typing you must do to specify filenames on a command line.
The process by which the shell interprets these characters as the full filenames they represent is known as filename expansion. File name expansion is a useful mechanism when you want to specify many files on a single command line. For example, you might want to print a group of files containing records for the month of December, all of which begin with the letters dec. By using one of these special characters to represent the parts of the filenames that vary, you can type one print command and specify all the files that begin with dec, thus avoiding the need to type the full names of all the desired files on the command line.
This section explains how to use the asterisk, question mark, and brackets for filename expansion.
The asterisk (*) matches any string of characters, including a null (empty) string. You can use the * to specify a full or partial filename. The * alone matches all the file and directory names in the current directory, except those starting with a . (dot). To see the effect of the *, try it as an argument to the echo(C) command. Type:
echo *The echo command displays its arguments on your screen. Notice that the system response to echo * is a listing of all the filenames in your current directory.
``Summary of filename generation characters'' summarizes the syntax and capabilities of the echo command.
For another example, say you have written several reports and have named them report, report1, report1a, report1b.01, report25, and report316. By typing report1* you can refer to all files that are part of report1, collectively. To find out how many reports you have written, you can use the ls command to list all files that begin with the string report, as shown in the following example.
$ ls report* report report1 report1a report1b.01 report25 report316 $The * matches any characters after the string report, including no letters at all. Notice that * matches the files in numerical and alphabetical order. A quick and easy way to display the contents of your report files in order on your screen is by typing the following command:
pr report*Now try another exercise. Suppose you have a current directory called appraisals that contains files called Andrew_Adams, Paul_Lang, Jane_Peters, and Fran_Smith, choose a character that all the filenames in your directory have in common, such as a lowercase ``a.'' Then request a listing of those files by referring to that character. For example, if you choose a lowercase ``a,'' type the following command line:
ls *a*The system responds by printing the names of all the files in your current directory that contain a lowercase ``a.''
The ``*'' can represent characters in any part of a filename. For example, if you know the first and last letters are the same in several files, you can request a list of them on that basis. If, for example, you had a directory containing files named FATE, FE, FADED_LINE, F123E, Fig3.4E, FIRE_LANE, FINE_LINE, FREE_ENTRY, and FAST_LANE, you could use this command to obtain a list of files starting with ``F'' and ending with ``E.'' For such a request, your command line might look like this:
ls F*EThe system response will be a list of filenames that begin with F, end with E, and are in the following order:
F123E FADED_LINE FAST_LANE FATE FE FINE_LINE FIRE_LANE Fig3.4E
The order is determined by the collating sequences of the language being used, in this case, English: (1) numbers, (2) uppercase letters, (3) lowercase letters.
The ``*'' is even more powerful; it can help you find all files named memo in any directory one level below the current directory:
ls */memo
The question mark (?) matches any single character of a filename except a leading period (.). Let us suppose you have written several chapters in a book that has 12 chapters, and you want a list of those you have finished through Chapter 9. If your directory contains the following files:
chapter1 chapter2 chapter5 chapter9 chapter11use the ls command with the ``?'' to list all chapters that begin with the string ``chapter'' and end with any single character, as shown below:
$ ls chapter? chapter1 chapter2 chapter5 chapter9 $The system responds by printing a list of all filenames that match.
Although ``?'' matches any one character, you can use it more than once in a filename. To list the rest of the chapters in your book, type:
ls chapter??Of course, if you want to list all the chapters in the current directory, use the ``*'' (asterisk):
ls chapter*
Use brackets ([ ]) when you want the shell to match any one of several possible characters that may appear in one position in the filename. Suppose your directory contains the following files: cat, fat, mat, rat. If you include [crf] as part of a filename pattern, the shell will look for filenames that have the letter ``c,'' ``r,'' or ``f'' in the specified position, as the following example shows.
$ ls [crf]at cat fat rat $This command displays all filenames that begin with the letter ``c,'' ``r,'' or ``f,'' and end with the letters ``at.'' Characters that can be grouped within brackets in this way are collectively called a ``character class.''
Brackets can also be used to specify a range of characters, whether numbers or letters. Suppose you have a directory containing the following files: chapter1, chapter2, chapter3, chapter4, chapter5 and chapter6. If you specify
chapter[1-5]the shell will match the files named chapter1 through chapter5. This is an easy way to handle only a few chapters at a time.
Try the pr command with an argument in brackets:
$ pr chapter[2-4]This command displays the contents of chapter2, chapter3, and chapter4, in that order, on your terminal.
A character class may also specify a range of letters. If you specify [A-Z], the shell will look only for uppercase letters; if [a-z], only lowercase letters.
The functions of these special characters are summarized in ``Summary of filename generation characters''. Try to use them on the files in your current directory.
Summary of filename generation characters
Character | Function |
---|---|
Match any string of characters (including an empty, or null string) except a leading period. | |
? | Match any single character, except a leading period. |
[xyz] | Match one of the characters specified within the brackets. |
[a-z] | Match one of the range of characters specified. |