Section 5.7. Filename Substitution (Wildcards)


[Page 174 (continued)]

5.7. Filename Substitution (Wildcards)

All shells support a wildcard facility that allows you to select files from the file system that satisfy a particular name pattern. Any word on the command line that contains at least one of the wildcard metacharacters is treated as a pattern, and is replaced by an alphabetically sorted list of all the matching filenames. This act of pattern replacement is called globbing. The wildcards and their meanings listed in Figure 5-6.


[Page 175]

Figure 5-6. Shell wildcards.

Wildcard

Meaning

*

Matches any string, including the empty string.

?

Matches any single character.

[..]

Matches any one of the characters between the brackets. A range of characters may be specified by separating a pair of characters by a dash.


You may prevent the shell from processing the wildcards in a string by surrounding the string by single quotes (apostrophes) or double quotes. See the "quoting" section later in this chapter for more details. A / character in a filename must be matched explicitly. Here are some examples of wildcards in action:

$ ls -FR              ...recursively list my current directory. a.c   b.c   cc.c    dir1/  dir2/ dir1: d.c  e.e dir2: f.d  g.c $ ls *.c                    ...any text followed by ".c". a.c   b.c   cc.c $ ls ?.c                    ...one character followed by ".c". a.c  b.c $ ls [ac]*            ...any string beginning with "a" or "c". a.c   cc.c $ ls [A-Za-z]*        ...any string beginning with a letter. a.c   b.c   cc.c $ ls dir*/*.c         ...all ".c" files in "dir*" directories. dir1/d.c  dir2/g.c $ ls */*.c            ...all ".c" files in any subdirectory. dir1/d.c  dir2/g.c $ ls *2/?.? ?.? a.c   b.c   dir2/f.d  dir2/g.c $ _ 


The result of a pattern that has no matches is shell-specific. Some shells have a mechanism for turning off wildcard replacement.

5.7.1. Pipes

The shell allows you to use the standard output of one process as the standard input of another process by connecting the processes together using the pipe (|) metacharacter. The sequence

$ command1 | command2 



[Page 176]

causes the standard output of command1 to "flow through" to the standard input of command2. Any number of commands may be connected by pipes. A sequence of commands chained together in this way is called a pipeline.

Pipelines support one of the basic Linux philosophies, which is that large problems can often be solved by a chain of smaller processes, each performed by a relatively small, reusable utility.

The standard error channel is not piped through a standard pipeline, although some shells support this capability.

In the following example, I piped the output of the ls utility to the input of the wc utility to count the number of files in the current directory. See Chapter 3, "GNU Utilities for Nonprogrammers" for a description of wc.

$ ls            ...list the current directory. a.c   b.c   cc.c   dir1   dir2 $ ls | wc -w    ...count the entries.       5 $ _ 


Figure 5-7 illustrates the pipeline.

Figure 5-7. A simple pipeline.


In the next example, I piped the contents of the "/etc/passwd" file into the gawk utility to extract the first field of each line. The output of gawk was then piped to the sort utility, which sorted the lines alphabetically. The result was a sorted list of every user on the system. The gawk utility is described fully in Chapter 4, "GNU Utilities for Power Users."

$ head -4 /etc/passwd      ...look at the password file. root:eJ2S10rVe8mCg:0:1:Operator:/:/bin/csh nobody:*:65534:65534::/: daemon:*:1:1::/: sys:*:2:2::/:/bin/csh $ cat /etc/passwd | gawk -F: '{ print $1 }' | sort ables adm bin daemon glass mail news 
[Page 177]
nobody root sync tim uucp $ _


Figure 5-8 illustrates the pipeline:

Figure 5-8. A pipeline that sorts.


There's a very handy utility called tee that allows you to copy the output of a pipe to a file and still allow it to flow down the pipeline. As you might have guessed, the name of this utility comes from the "T" connections that plumbers use. Figure 5-9 describes how tee works.

Figure 5-9. Description of the tee command.

Utility: tee -ia {fileName}+

The tee utility copies its standard input to the specified files and to its standard output. The -a option causes the input to be appended to the files rather than overwriting them. The -i option causes interrupts to be ignored.


In the following example, I copied the output of who to a file called "who.capture," and also let it pass through to sort:

$ who | tee who.capture | sort ables    pts/6   May  3 17:54 (gw.waterloo.com) glass    pts/0   May  3 18:49 (blackfoot.utdall) posey    pts/2   Apr 23 17:44 (:0.0) posey    pts/4   Apr 23 17:44 (:0.0) $ cat who.capture           ...look at the captured data. glass    pts/0   May  3 18:49 (blackfoot.utdalla) posey    pts/2   Apr 23 17:44 (:0.0) posey    pts/4   Apr 23 17:44 (:0.0) ables    pts/6   May  3 17:54 (gw.waterloo.com) $ 


Notice that the output captured is directly from the who utility before the list is sorted.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net