≡ Menu

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

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

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 12 comments… add one }
  • Sayed Ahmad February 12, 2012, 12:11 am

    What this mean?
    $ command > file-name 2>&1

    • Hesham M January 22, 2014, 3:34 pm

      This means redirect stdout to file-name, with that in mind redirect stderr t stdout.
      This will lead to both stderr and stdout go to file-name.

  • Shane Hathaway February 24, 2012, 1:02 am

    Sayed: that line means execute the command while redirecting both stdout and stderr to a file given by file-name.

    • RudyD April 2, 2012, 12:47 pm


      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.

      • TodorMinchev May 14, 2013, 9:03 pm


        • Daniel August 26, 2013, 7:22 pm

          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.

          • Sekkuar September 2, 2013, 7:20 pm

            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

  • iek September 24, 2012, 7:11 am

    I like the &>file one. but not for every stiuation.

  • Matt Kukowski January 29, 2014, 6:33 pm

    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.

  • iamfrankenstein June 12, 2014, 8:35 pm

    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 :)

  • Alejandro April 22, 2015, 4:41 pm

    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!

  • AAA December 4, 2016, 2:15 pm

    Kewl, thx

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , , , , , , , , ,