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

by on March 12, 2008 · 10 comments· LAST UPDATED March 12, 2008

in , ,

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:

HandleNameDescription
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
OR
$ 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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 10 comments… read them below or add one }

1 Sayed Ahmad February 12, 2012 at 12:11 am

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

Reply

2 Hesham M January 22, 2014 at 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.

Reply

3 Shane Hathaway February 24, 2012 at 1:02 am

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

Reply

4 RudyD April 2, 2012 at 12:47 pm

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.

Reply

5 TodorMinchev May 14, 2013 at 9:03 pm

RudyD
+1
:)

Reply

6 Daniel August 26, 2013 at 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.

Reply

7 Sekkuar September 2, 2013 at 7:20 pm

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
http://wiki.bash-hackers.org/howto/redirection_tutorial

Reply

8 iek September 24, 2012 at 7:11 am

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

Reply

9 Matt Kukowski January 29, 2014 at 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.

Reply

10 iamfrankenstein June 12, 2014 at 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 :)

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: