dcsimg
Linux Today: Linux News On Internet Time.




More on LinuxToday


About.com: Using pipes in Linux

Oct 14, 2000, 16:30 (1 Talkback[s])
(Other stories by Aron Hsiao)

WEBINAR:
On-Demand

Re-Imagining Linux Platforms to Meet the Needs of Cloud Service Providers


"If you're a novice shell user, you've probably heard of pipes but don't really know what they are or how to use them. A pipe is a special line of communication opened by the shell between one program's output and another program's input. In the shell, a pipe is represented by the vertical bar (|) character."

"To connect a command's output to another command's input using a pipe, place a vertical bar between the two commands. For example, to get a directory listing but send it to the printer instead of to the display, try:

  $ ls -1 | lpr
In this case, the output of the ls command goes to the the lpr command, which uses it as input and sends it to the printer."

"Sometimes, very complex tasks can be performed by connecting long strings of pipes together:

  $ ls -1 | grep txt | grep -v jake | cut -d. -f1 | sort | lpr
This series of commands first gets a list of files in the current directory with ls. This list is sent to the grep command, which searches for the text "txt" in the filenames. The list of filenames containing the word "txt" is then sent to the grep command again, this time to filter out any names containing the text "jake" from the list. This shortened list is then sent to the cut command, which removes all "extensions" from the filenames. The list is now sent to sort, which rearranges the list into alphabetical order before finally sending it to lpr which will send the list to the printer. Whew!"

Complete Story

Related Stories: