Redirect error messages in Unix

This information pertains primarily to Bourne-like shells, such as sh, ksh, and bash.

To redirect standard error messages to a file, enter:

  command 2> file

Replace command with the command you want to execute and file with the file to which you want to direct the errors, for example:

  gunzip * 2> ~/errors

If you wish to suppress error messages, enter:

  command 2>&-

If you wish to redirect error messages to standard output, enter:

  command 2>&1

You can direct both standard error and standard out messages to an output file by entering the following:

  command > file 2>&1

The csh and tcsh shells have substantially less flexible redirection capabilities. However, from sh or tcsh you can invoke a Bourne-like shell to run a command that redirects standard error messages. To redirect standard error messages, enter the following:

  ksh -c 'commands'

You can use sh or bash in the place of ksh. Replace commands with redirection syntax, for example:

  ksh -c 'ls -l foo 2> ~/notfound'

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document agvw in the Knowledge Base.
Last modified on 2019-01-09 10:22:58.

Contact us

For help or to comment, email the UITS Support Center.