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)
stderr – Use to write error message (screen)
Understanding I/O streams numbers
The Unix / Linux standard I/O streams with numbers:
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[/code]
Redirecting the standard error (stderr) and stdout to file
Use the following syntax:$ command-name &>file[/code]
OR$ command > file-name 2>&1[/code]
Another useful example:# find /usr/home -name .profile 2>&1 | more[/code]
Redirect stderr to stdout
Use the command as follows:$ command-name 2>&1[/code]
Joel G Mathew, known in tech circles by the pseudonym Droidzone, is an opensource and programming enthusiast.
His favorite pastime is grappling with GNU compilers, discovering newer Linux secrets, writing scripts, hacking roms, and programs (nothing illegal), reading, blogging. and testing out the latest gadgets.
When away from the tech world, Dr Joel G. Mathew is a practising ENT Surgeon, busy with surgeries and clinical practise.