The ‘>’ operator is used to redirect the output to a new file, the ‘>>’ is used to redirect the output and append to the file.
Now both the STDOUT and STDERR are written to the console by default. Output from a console (Command Prompt) application or command is often sent to two separate streams. The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the “>” symbol, you are only redirecting STDOUT. In order to redirect STDERR you have to specify “2>” for the redirection symbol. This selects the second output stream which is STDERR.
For Example, running the dir command on a file gives both stdout and stderr like below:
1 2 3 4 5 6 7
C:\ dir nosuchfile.txt Volume in drive C has no label. Volume Serial Number is B8A1-5FD1 Directory of C:\ File Not Found
Here the “File Not Found” message is the STDERR and the rest was for STDOUT. Now if you want to redirect the whole output to a file, just running the command dir nosuchfile.txt > result.log will not cut it. You need to do one of the following:
Sending the STDERR and STDOUT to different files:
dir nosuchfile.txt > out.log 2>error.log
Sending the STDERR and STDOUT to Same file:
dir nosuchfile.txt > alloutput.log 2>&1
Here the 2>&1 instructs that the STDERR to be redirected to STDOUT which is in-turn writing out to alloutput.log file.