How to use standard files in shell commands



Last revision August 3, 2004

Table of Contents:

  1. The Unix file system
  2. The directory tree
  3. File ownership and permissions
  4. Files as units
  5. Examining file contents
  6. Other commands
  7. Standard Files and Data Pipes

Special characters (metacharacters) typed as part of commands are interpreted by the shell as instructions to redirect the standard input, output, or error:

<
redirects standard input from an existing file. Usage:
      program < infile

The input file must already exist or there will be an error.

>
redirects standard output to a new file. Usage:
      program > outfile

A new output file is created; if one already exists with the same name, it is silently removed first. To prevent this anti-social behavior, use set noclobber in your .login file (this is the default setting for new pangea accounts). Then the command will abort rather than over-write the existing file.

>>
redirects standard output to be appended to an existing file. Usage:
      program >> outfile

If the output file does not exist already, you will get an error.

Standard error can not be independently redirected (in the C-shell). It can be redirected together with the standard output, however. Just add the ampersand metacharacter (&) after the > or >> that is redirecting the standard output, for example, >& or >>&

You can put both types of redirection together for a command that reads from standard input and writes to standard output:

program < infile > outfile

<--Previous Overview Next-->

Comments or Questions?