[ Team LiB ] |
13.2 Filename GlobbingBefore the shell passes arguments to an external command or interprets a built-in command, it scans the command line for certain special characters and performs an operation known as filename globbing. Filename globbing resembles the processing of wildcards used in MS-DOS commands, but it's much more sophisticated. Table 13-1 describes the special characters, known as filename metacharacters, used in filename globbing.
In filename globbing, just as in MS-DOS wildcarding, the shell attempts to replace metacharacters appearing in arguments in such a way that arguments specify filenames. Filename globbing makes it easier to specify names of files and sets of files. For example, suppose the current working directory contains the files file1, file2, file3, and file04. Suppose you want to know the size of each file. The following command reports that information: ls -l file1 file2 file3 file04 However, the following command reports the same information and is much easier to type: ls -l file* As Table 13-1 shows, the * filename metacharacter can match any string of characters. Suppose you issued the following command: ls -l file? The ? filename metacharacter can match only a single character. Therefore, file04 would not appear in the output of the command. Similarly, the command: ls -l file[2-3] would report only file2 and file3, because only these files have names that match the specified pattern, which requires that the last character of the filename be in the range 2-3. You can use more than one metacharacter in a single argument. For example, consider the following command: ls -l file?? This command will list file04, because each metacharacter matches exactly one filename character. Most commands let you specify multiple arguments. If no files match a given argument, the command ignores the argument. Here's another command that reports all four files: ls -l file0* file[1-3]
The tilde (~) metacharacter lets you easily refer to your home directory. For example, the following command: ls ~ would list the files in your home directory. Filename metacharacters don't merely save you typing. They let you write scripts that selectively process files by name. You'll see how that works later in this chapter. |
[ Team LiB ] |