Course: ITNW6088 UNIX for Webmasters
Instructor:
Ashley Rosilier unix@iteachu.com

Dates: Jan. 31 - Mar. 11

Class Quick Links
Syllabus Roster Grades Schedule Forums Chat Lessons Projects
Class is self-paced
 
On-line Tutorials College Credit Continuing Ed. Certifications

Home > Continuing Education > UNIX > Lessons > Lesson 4

 

Lesson 4 - Redirecting I/O

Required Reading

  1. Learning UNIX: Chapter 5 (Redirecting I/O)

Additional Notes

A related concept to standard output is standard error. In addition to displaying results of a command, the UNIX shell sometimes prints error messages that result from a command. Any time you get an error message printed to the terminal, that is considered standard error, and not standard out. If you only redirect standard output, you will still see error messages on the screen.

As you learned in your text, you can redirect standard out using the > operator. Standard error, on the other hand, can be redirected using the operator 2>. If you want to combine both standard out and standard error, you can generally use the operator >&. However, there are some variations depnding on which UNIX flavor and shell you are using. For more variations review the section on I/O redirection (5.4.2) in this article.

The exercise below will help clarify these differences. This exercise is should be submitted as part of project 3.

Task Command Comments
list the contents of two directories (one is valid, one is invalid) ls . jke Notice that you get an error message on the screen as well as the directory listing.
redirect standard out to a file

ls . jke > myoutput

cat myoutput

The error message is still displayed on the screen; it is not written to the file with the listing results.
redirect standard error to a file

ls . jke 2> myerrors

cat myerrors

The error message is written to the file, while the listing results are displayed on the screen.
redirect both standard error and standard out to different files

ls . jke > myoutput 2> myerrors

cat myerrors

cat myoutput

The errors are written to the file myerrors while the listing results are written to the file myoutput.
redirect both standard error and standard out to the same file

ls . jke >& myoutput

cat myoutput

The error message is written to the file myoutput, along with the directory listing.

One nice thing about output redirection is that commands run a whole lot faster when they aren't printing results to the screen.


An interesting filter is the wc (word count) command. Here's the syntax (from man wc)

NAME wc - print the number of bytes, words, and lines in files SYNOPSIS wc [-clw] [--bytes] [--chars] [--lines] [--words] [--help] [--version] [file...]

DESCRIPTION wc counts the number of bytes, whitespace-separated words, and newlines in each given file, or the standard input if none are given or when a file named `-' is given. It prints one line of counts for each file, and if the file was given as an argument, it prints the filename following the counts. If more than one filename is given, wc prints a final line containing the cumulative counts, with thefilename `total'. The counts are printed in the order: lines, words, bytes. By default, wc prints all three counts. Options can specify that only certain counts be printed. Options do not undo others previously given, so wc --bytes --words prints both the byte counts and the word counts.

OPTIONS

-c, --bytes, --chars
Print only the byte counts.

-w, --words
Print only the word counts.

-l, --lines
Print only the newline counts.


The text discussion of the grep command is not very clear about the different uses of grep. They do not elaborate on how grep works without the use of pipes.

grep "cat" file.txt

will search the contents of the file "file.txt" for the word "cat". That is different than the use of grep with a pipe. When you use grep without a pipe, it searches file contents. If you pipe something to grep, it searches the text that you are piping to standard input instead. Therefore, if you do the ls command and pipe it to grep (as in the book examples) you are searching the text that is returned by the ls command.

So

grep "test" *

searches the contents of all files in the current directory for the word "test". This would find a list of files in the working directory that contain a the word "test" within their text.

where

ls * | grep "test"

searches the filenames of all files in the current directory for the word "test". This would find, for example, a file named "testfile.txt" in the working directory.

It's a subtle difference, but very important!

Next Step

Take the self-quiz

Complete project 3

Continue to lesson 5

 

 

Last update: 01-Feb-2005 0:14

 
About iTeachU
Programs Offerred
Library
Book Store
Other Resources

Course Access

 

 

 

Home > Continuing Education > UNIX > Lessons > Lesson 4

On-line Tutorials College Credit Continuing Ed. Certifications

 

info@iteachu.com

© Copyright 2001-2002 iTeachU All Rights Reserved