How do I redirect stdout and stderr to a file?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux 3
  • Red Hat Enterprise Linux 4
  • Red Hat Enterprise Linux 5

Issue

How do I redirect stdout and stderr to a file?

Resolution

You can redirect stdout and stderr several different ways. Below you will find several scenarios that show you how to execute the specified task from the command line with examples of each:

To redirect stdout to a file:

 <command> > <filename>
 ls -l > ls-l.txt

To redirect stderr to file:

 <command> 2> <filename>
 grep da * 2> grep-errors.txt

To redirect stdout to stderr:

 <command> 1>&2
 grep da * 1>&2

To redirect stderr to stdout:

 <command> 2>&1
 grep * 2>&1

To redirect stderr and stdout to file:

 <command> &> <filename>
 rm -f $(find / -name core) &> /dev/null
SBR
Components
Category
Tags

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.