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

by on March 12, 2008 · 5 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



If you would like to be kept up to date with our posts, you can follow us on Twitter, Facebook, Google+, or even by subscribing to our RSS Feed.


{ 5 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 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

3 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

4 TodorMinchev May 14, 2013 at 9:03 pm

RudyD
+1
:)

Reply

5 iek September 24, 2012 at 7:11 am

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

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

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

Previous Faq:

Next Faq: