Prev | Current Page 186 | Next

Christopher Negus

"Linux Bible, 2008 Edition: Boot up to Ubuntu, Fedora, KNOPPIX, Debian, openSUSE, and 11 Other Distributions"


Piping Commands
The pipe (|) metacharacter connects the output from one command to the input of another command.
This lets you have one command work on some data, and then have the next command
deal with the results. Here is an example of a command line that includes pipes:
$ cat /etc/password | sort | less
This command lists the contents of the /etc/password file and pipes the output to the sort
command. The sort command takes the usernames that begin each line of the /etc/password
file, sorts them alphabetically, and pipes the output to the less command (to page through the
output).
Pipes are an excellent illustration of how UNIX, the predecessor of Linux, was created as an operating
system made up of building blocks. A standard practice in UNIX was to connect utilities in different
ways to get different jobs done. For example, before the days of graphical word processors,
users created plain-text files that included macros to indicate formatting. To see how the document
really appeared, they would use a command such as the following:
$ gunzip < /usr/share/man/man1/grep.1.gz | nroff -c -man | less
In this example, the contents of the grep man page (grep.1.gz) are directed to the gunzip command
to be unzipped. The output from gunzip is piped to the nroff command to format the
man page using the manual macro (-man).


Pages:
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198