Pipes and InputOutput Redirection


Pipes and Input/Output Redirection

One of the things that makes UNIX so powerful is the fact that output from one command can be used as the input to another command, and the final output can be redirected to other places than just the screen. You can get a listing of files or the output of a program, pipe it into grep to filter out irrelevant lines, and then send it through fmt to process it for easy readingall on the same command line. These kinds of tasks are very frequently useful in system administration tasks such as log file processing and filesystem auditing. For example, ls would normally display the directory list to the screen, but it can easily have that output redirected to a file, like this:

# ls > filelist.txt


This will create a text file called filelist.txt that contains the directory list of the current directory. This is known as output redirection.

If the directory list is too long to fit on the screen, you can pipe its output to the less program, like this:

# ls | less


You may recall that the less command displays text sent into it one screen at a time. This will prevent the directory list from scrolling off the screen before you get a chance to read it.

What if I want to mail the Albert Schweitzer quote to someone? Rather then type the quote into an email message, you can send the file that contains the quote as the input to the mail command. There are actually two ways you can do this, and both have the same effect. First, you can use the cat ("catenate") command, which would normally print the file to the screen and pipe the output to the mail program, like this:

# cat quote.txt | mail useraddress


The following command uses input redirection to accomplish the same thing:

# mail useraddress < quote,txt


In this case, you have told the mail command that instead of getting the message to send from the keyboard, it should get the message from the file quote.txt. Although both these commands accomplish the same thing, the second one is more efficient because it doesn't have to call the cat program. Instead, it lets the shell handle redirection.

You can do both input and output redirection in the same command. For example, the TR command you used previously tells TR to get its input from file1 and send its output to file2:

# tr 'a-z' 'A-Z' < file1 > file2


Tip

If you ever get confused about whether to use < or >, remember that the arrow points in the direction the data is going.


You can combine multiple pipes into a single command:

# cut -f 1 -d ' ' access.log | sort | uniq -c | more


This is a quick-and-dirty way of extracting useful information from an NCSA-compliant web server log file. Specifically, this information extracts field 1, which contains the network address of each hit, pipes it to sort, and then pipes the sorted output to uniq -c. This counts the number of occurrences of identical lines and then displays each unique line, preceded by the number of times that line was repeated, and then pipes the output to more so that you can read it without it scrolling off the screen. Specifically, this command tells me how many hits each network address generated on my web server.

You can also combine pipes and input/output redirection:

# cut -f 1 -d ' ' /var/log/httpd-access.log | sort | uniq -c > hits.txt


This is the same as the first command, except it records the information in a file instead of displaying it on the screen.

The creative use of pipes and input/output redirection is where the power of FreeBSD is really unleashed. (You can make the previous command more useful using awk, but you will have to wait until Chapter 10 to learn how.)




FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

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