I am trying to redirect bash message into file named output.log. But, it is not getting redirected. How do I redirect both standard output and standard error in bash shell? In Linux, how do I redirect error messages?
Standard error (also known as stderr) is the default error output device. Use stderr to write all system error messages. The number two (2) denotes the stderr. The default stderr is the screen or monitor. Standard output (also known as stdout) is used by a command to writes (display) its output. The default stdout is the screen. It is denoted by one number (1).
2> is input redirection symbol and syntax is:
To redirect stderr (standard error) to a file: command 2> errors.txt
To redirect both stderr and stdout (standard output): command &> output.txt
You must replace command with the command you want to run. Let us see some examples that explains redirection of standard error in bash.
How to redirect standard error in bash
Run find command and save all error messages to find.error.txt file: find / -name "*.conf" 2> find.error.txt You can view find.error.txt with the cat command: cat find.error.txt Sample outputs:
You need to use “2>” when you want to redirect stderr to a file. You can redirect stdout to file named results.txt and stderr to file named errors.txt: find / -name "*.conf" >results.txt 2>error.txt Verify results with the cat command: cat results.txt cat error.txt This is useful in shell scripts or any other purpose.
How to redirect standard error and standard output in bash
You can send both stdout and stderr to a file named output.txt command &>output.xt find / -name "*.pl" &>filelist.txt Please note that both errors and actual output of the find command stored into a file: cat filelist.txt Sample outputs:
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.