BASH Shell: How To Redirect stderr To stdout ( redirect stderr to a File )

Posted on in Categories , , last updated March 12, 2008

Q. How do I redirect stderr to stdout? How do I redirect stderr to a file?

A. Bash and other modern shell provides I/O redirection facility. There are 3 default standard files (standard streams) open:
[a] stdin – Use to get input (keyboard) i.e. data going into a program.

[b] stdout – Use to write information (screen)

[c] stderr – Use to write error message (screen)

Understanding I/O streams numbers

The Unix / Linux standard I/O streams with numbers:

0 stdin Standard input
1 stdout Standard output
2 stderr Standard error

Redirecting the standard error stream to a file

The following will redirect program error message to a file called error.log:
$ program-name 2> error.log
$ command1 2> error.log

Redirecting the standard error (stderr) and stdout to file

Use the following syntax:
$ command-name &>file
$ command > file-name 2>&1
Another useful example:
# find /usr/home -name .profile 2>&1 | more

Redirect stderr to stdout

Use the command as follows:
$ command-name 2>&1

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

12 comment

    1. Greetings!

      A slightly more correct is:
      The output of the ‘command’ is redirected to a ‘file-name’ and the error chanel (that is the ‘2’ is redirected to a pointer (?) of the output (‘&1’).
      So stderr goes to the stdout and that goes to the file.

        1. Actually it means “first redirect STDERR to STDOUT, so any errors printed out on STDERR should go to STDOUT. Then, execute ‘command’ and redirect its STDOUT to ‘file-name'” – keeping in mind that at this point STDOUT will also contain whatever is written to STDERR because of the earlier redirection.

          1. Incorrect.
            There are two incorrect concepts in your answer.

            First is: the redirection happens from left to right. This means that the STDOUT is redirected first.
            (When you have > without a stream number, it actually have an implicit 1)
            And only after STDERR is redirected to “the same place STDOUT is pointing”, meaning, ‘file-name’

            Second wrong concept with your answer is: There are no connection between the descriptors. Changing STDOUT after STDERR had been redirected to STDOUT won’t change STDERR.
            It will make STDERR point to STDOUT and then change STDOUT to something else (without touching STDERR)
            Here is a more detailed tutorial covering both those misconceptions

  1. In pre-bash4 days you HAD to do it this way:

    cat file > file.txt 2>&1

    now with bash 4 and greater versions… you can still do it the old way …but …

    cat file &> file.txt

    The above is bash4+ … some OLD distros may use prebash4 but I think they are alllong gone by now. Just something to keep in mind.

  2. I really love: “command2>&1 | tee logfile.txt”

    because tee log’s everything and prints to stdout . So you stil get to see everything! You can even combine sudo to downgrade to a log user account and add date’s subject and store it in a default log directory :)

  3. Hi! good explanation, I’d like to make a function on C that redirects STDIN and SDTOUT to an script, how can I do that, I mean, the exist a library’s on C to put terminal sintaxis on C?, how would you start to do it? I’m very lost with this. Thankyou!

Leave a Comment