Pipes And Redirection
Most commands take data from the standard input (usually the keyboard), process or filter it, and send the results to the standard output (usually the display). Pipes and redirection provide the capability to take input from something other than the keyboard and send the output to something other than the display. Pipes are indicated by the symbol | (this is not a lowercase L but the vertical bar character). Using a pipe, the standard output of one command can be redirected to become the standard input for subsequent command. For example:
ls -al |grep file1
This will perform the long form of the ls command, showing hidden files, and send the output of that command to the grep command, which will then filter that output and only show lines that have the string or pattern file1 in them. This would perform approximately the same function as:
ls -al file1
Pipes perform a very useful function in Linux and other uses will be discussed in the section about administrative commands. Using pipes many commands can be strung together to perform very powerful functions. Linux has many text processing commands that can be used in an interesting manner by piping the output of one command into another.
The use of the > for redirecting output was shown with the cat command where two files were sent one after another to a third location. This was:
cat file1 file2 > file3
In this manner instead of sending file1 then file2 to the screen, the output was redirected and stored in file3. The > sign will redirect the standard output to the file named after the >. It is a important remember that if file3 exists and has contents, the first action will be to delete those contents before copying file1 to files3, then appending the contents of file2 to the new contents of file3.
The >> can be used prevent over writing the file. It will redirect the standard output to the file but append that output to the end of the indicated file. So in the above example if >> had been used instead of > the contents of file1 would have been appended to the end of the contents of file3 and then the contents of file2 would have been appended to the end of everything.
One final redirection operator is the < sign. This will redirect the standard input so that it will come from the file given after the < sign instead of the keyboard. This can be used when you want to input a file to a command that would not normally take a file as its input.
Home - Table Of Contents - Contact Us
CertiGuide to A+ (A+ 4 Real) (http://www.CertiGuide.com/apfr/) on CertiGuide.com
Version 1.0 - Version Date: March 29, 2005
Adapted with permission from a work created by Tcat Houser et al.
CertiGuide.com Version © Copyright 2005 Charles M. Kozierok. All Rights Reserved.
Not responsible for any loss resulting from the use of this site.