University of Southern California

ITS Information Technology Services

A division of the Office of the Chief Information Officer

Redirecting Input and Output

File Redirection

The output from most commands is sent to standard output, usually the screen. You can, however, redirect output from a command into a file instead of to your screen.

To redirect output to a file, you would type:

Some examples:

In the above examples, if the specified file already existed, it would be overwritten. If you want to append output to a currently existing file instead of overwritting it, you would type:

For example:

The input to most commands comes from standard input, usually the keyboard. You can however, redirect input to a command to come from a file rather than the keyboard.

To redirect input to a command, you would type:

Some examples:

You can also redirect standard output to a file while redirecting standard error to the screen by typing:

For example, if you were running a C or fortran program and you wanted the results to go to a file, but the error messages to go to the screen, you could type:

Output Redirection

The previous section describes redirecting output to file or input from a file. You can also redirect the output of one command into the input of another command by using the | symbol-- the "pipe" symbol. You would type:

Some examples:

You can also redirect the output of a command to a file and to the screen or another command using the tee command. You would type:

The first example will send a copy of the output to a file and a copy of the output to the screen. The second example will send a copy of the output to a file and send a copy of the output on to the next command.

Some examples:

Last updated:
February 03, 2011

UNIX Documentation

The use of all USC computing resources is governed by the USC Computing Policies.