By default, the output of a Unix command or program may be written to either standard output or standard error. Both of these streams are sent to the terminal. In some cases, you may be interested in redirecting the output to a file, as opposed to having it appear on the terminal. In this way, you could view or edit the file later.
Re-directing from a File
The drivers used to test your code for ECE 250 read input from standard input (console input or cin) which is, by default, the keyboard.
You can redirect the input read by standard input by using the <. The input is treated as if you were typing the file.
Re-directing to a File
To redirect the output of a Unix command or a program to a file, append > filename to the command line, for example:
{ecelinux:1} ls > tmpfile {ecelinux:2}
This creates a file tmpfile and writes the output of ls to that file. If you try this again, you will get an error:
{ecelinux:2} ls > tmpfile tmpfile: File exists.
You may either delete the old file (rm) or use ! to indicate that you want to overwrite the already-existing file:
{ecelinux:3} ls >! tmpfile tmpfile: File exists.
Appending to a File
If you want to append the output of a Unix command or program to a file, use >> filename where filename is an already-existing file (you can use >>! filename if you want it to create a file if it doesn't already exist). This will append the output to the end of the given file. For example,
{ecelinux:1} ls > tmpfile {ecelinux:2} date >> tmpfile
appends the output of date to a file which already has a listing of the working directory.
A more useful command may be:
{ecelinux:1} grep int SingleList.h >! tmpfile
This prints all the lines in SingleList.h which contain the string int. The grep command is explained later, however, while most Unix users will consider grep to be the most useful command in existence, most beginners do not understand its purpose.
Re-directing (or Piping) Output as Input to Another Unix Command
You may recall that more does the same thing as cat (prints a file to the terminal), however, more pages the text, allowing you to read the output one page at a time. Suppose you wish to page a long directory listing, for example:
{ecelinux:1} ls /home | more
This will pipe the output from ls and send it to more, which will display the files in the directory one page at a time.
Redirecting or Piping the Error Stream
When a response is sent from the shell to the terminal, it may be sent on one of two streams:
- standard output, or
- standard error.
For example, commands like ls will send the directory listing on standard output, while compilation errors from g++ will be sent back on standard error.
The default is for both of these streams to printed to the terminal. The four operators listed above (>, >!, >>, and >>!) will only redirect the standard output stream. In the following example, we note that the output from ls is either printed to the screen or redirected to the file output, while the error message from mkdir when the directory is created twice is not redirected to output:
{ecelinux:1} ls images/ primary.0.html* t03/ t06/ t09/ t12/ t15/ t18/ index.content* t01/ t04/ t07/ t10/ t13/ t16/ t19/ index.html* t02/ t05/ t08/ t11/ t14/ t17/ t20/ {ecelinux:2} ls >! output {ecelinux:3} mkdir tmpdir {ecelinux:4} mkdir tmpdir mkdir: cannot create directory `tmpdir': File exists {ecelinux:5} mkdir tmpdir >! output mkdir: cannot create directory `tmpdir': File exists {ecelinux:6}
If you also want to redirect the standard error stream to a file, append the redirection or pipe operators with an &:
{ecelinux:6} mkdir tmpdir >&! output {ecelinux:7} more output mkdir: cannot create directory `tmpdir': File exists {ecelinux:8}
This is very useful if you want to redirect the error messages from a compilation attempt:
{ecelinux:1} g++ SingleListDriver.cpp >&! error_msgs {ecelinux:2} pico error_msgs
Summary
In this topic, we have covered output and error redirection to files. This is summarized in Table 1.
Table 1. Redirection and piping.
cmd > filename | Redirect the output to the file filename and generate an error if the file exists |
cmd >! filename | Redirect the output to the file filename overwriting the file if the file exists |
cmd >> filename | Append the output to the file filename and generate an error if the file does not exists |
cmd >>! filename | Append the output to the file filename and create a new file if the file does not exist |
cmd1 | cmd2 | Pipe the output to the command cmd2 |
cmd >& filename | Redirect the output and error messages to the file filename and generate an error if the file exists |
cmd >&! filename | Redirect the output and error messages to the file filename overwriting the file if the file exists |
cmd >>& filename | Append the output and error messages to the file filename and generate an error if the file does not exists |
cmd >>&! filename | Append the output and error messages to the file filename and create a new file if the file does not exist |
cmd1 |& cmd2 | Pipe the output and error messages to the command cmd2 |
Note: you are able to explicitly state which stream (standard output or standard error) any messages you print are sent on. By default, however, we will use standard output.
Copyright ©2005-2008 by Douglas Wilhelm Harder. All rights reserved.