Redirecting Input and Output in Unix
Posted On 2005-11-1 by FortyPoundHead
Keywords: Unix Tutorial Part 06
Tags: Tutorial Linux
Rating: / 5.00
Section 6: Redirecting Input and OutputCONCEPT:
Every program you run from the shell opens three files:
Standard input, standard output, and standard error. The files
provide the primary means of communications between the programs, and
exist for as long as the process runs.
The standard input file provides a way to send data to a process. As
a default, the standard input is read from the terminal keyboard.
The standard output provides a means for the program to output data.
As a default, the standard output goes to the terminal display screen.
The standard error is where the program reports any errors encountered
during execution. By default, the standard error goes to the terminal
A program can be told where to look for input and
where to send output, using input/output redirection
uses the "less than" and "greater than" special characters (< and
>) to signify input and output redirection, respectively.
Using the "less-than" sign with a file name like this:
in a shell command instructs the shell to read
input from a file called "file1" instead of from the keyboard.EXAMPLE:
Use standard input redirection to send the contents of
the file /etc/passwd to the more command:
- more <
Many UNIX commands that will accept a file name as a command line
argument, will also accept input from standard input if no file is
given on the command line.EXAMPLE:
To see the first ten lines of the /etc/passwd file, the
- head /etc/passwd
will work just the same as the
- head < /etc/passwd
Using the "greater-than" sign with a file name like this:
causes the shell to place the output from the
command in a file called "file2" instead of on the screen. If the
file "file2" already exists, the old version will be overwritten. EXAMPLE:
Type the command
- ls /tmp >
to redirect the output of the ls command into a
file called "ls.out" in your home directory. Remember that the tilde
(~) is UNIX shorthand for your home directory. In this command, the
ls command will list the contents of the /tmp directory.
Use two "greater-than" signs to append to an existing file. For
- >> file2
causes the shell to
append the output from a command to the end of a file called "file2".
If the file "file2" does not already exist, it will be created. EXAMPLE:
In this example, I list the contents of the /tmp
directory, and put it in a file called myls. Then, I list the
contents of the /etc directory, and append it to the file
- ls /tmp > myls
ls /etc >> myls
Redirecting standard error is a bit trickier, depending on the kind of
shell you're using (there's more than one flavor of shell program!).
In the POSIX shell and ksh, redirect the standard error with the
Sort the /etc/passwd file, place the results in a file
called foo, and trap any errors in a file called err with the
- sort < /etc/passwd > foo 2> err
Comments On This Post
No comments on this post yet!
Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.
Or you can drop a note to the administrators if you're not sure where you should post.