Use the Output of One Command As Input for Another


|

It's a maxim that Unix is made up of small pieces, loosely joined. Nothing embodies that principle more than the concept of pipes. A pipe is the | symbol on your keyboard, and when placed between two commands, it takes the output from the first and uses it as input for the second. In other words, | redirects stdout so it is sent to be stdin for the next command.

Here's a simple example that helps to make this concept clear. You already know about ls, and you're going to find out about the less command in Chapter 5, "Viewing Files." For now, know that less allows you to page through text files one screen at a time. If you run ls on a directory that has many files, such as /usr/bin, things just zoom by too fast to read. If you pipe the output of ls to less, however, you can page through the output one screen at a time.

$ pwd /usr/bin $ ls -1 zipinfo zipnote zipsplit zsoelim zxpdf [Listing truncated due to length - 2318 lines!] $ ls -1 | less 411toppm 7z 7za 822-date a2p 


You see one screen of results at a time when you pipe the results of ls -1 through to less, which makes it much easier to work with.

Here's a more advanced example that uses two commands discussed later: ps and grep. You'll learn in Chapter 9, "Finding Stuff: Easy," that ps lists running processes and in Chapter 12, "Monitoring System Resources," that grep helps find lines in files that match a pattern. Let's say that Firefox is acting strange, and you suspect that multiple copies are still running in the background. The ps command lists every process running on your computer, but the output tends to be lengthy and flashes by in an instant. If you pipe the output of ps to grep and search for firefox, you'll be able to tell immediately if Firefox in fact is still running.

Note

In order to save space in this listing, I removed the owner, which in every instance was the same person.


[View full width]

$ ps ux 1504 0.8 4.4 75164 46124 ? S Nov20 1:19 kontact 19003 0.0 0.1 3376 1812 pts/4 S+ 00:02 0:00 ssh admin@david.hartley.com 21176 0.0 0.0 0 0 ? Z 00:14 0:00 [wine-preloader] <defunct> 24953 0.4 3.3 51856 34140 ? S 00:33 0:08 kdeinit: kword /home/scott/documents/clientele /current [Listing truncated for length] $ ps ux | grep firefox scott 8272 4.7 10.9 184072 112704 ? Sl Nov19 76:45 /opt/firefox/firefox-bin


From 58 lines of output to onenow that's much easier to read!

Note

Keep in mind that many programs can work with pipes, but not all. The text editor vim (or pico, nano, or emacs), for instance, takes over the entire shell so that all input from the keyboard is assumed to be directed at vim, while all output is displayed somewhere in the program. Because vim has total control of the shell, you can't pipe output using the program. You'll learn to recognize non-pipable programs as you use the shell over time.




Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

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