How to redirect stdout / stderr to a file
Redirecting all your program output to a file is very simple and can be accomplished like this:
myapp > output.log
This line will send all the output generated by myapp to a file named output.log. The contents of output.log will always be overwritten; but you can use a second > to append to the current content like this:
myapp >> output.log
As well as with output, it is very useful to redirect the stderr (standard error output) to a file rather than to the screen. You can accomplish this right from the command line like this:
myapp 2> error.log
The previous line will redirect everything myapp sends to the stderr to a file named error.log. That is accomplished just by adding the “2>” operator. Every time this command is executed the error.log file will be recreated (the content will be deleted). As we did with output, if you want to append new lines to it, you have to use it the following way:
myapp 2>> error.log
You can also redirect both output and error in this way:
myapp > output.log 2> error.log
Or you can even send both streams to the same file:
myapp > all.log 2>&1
Finally, if you need to silence all errors, you can do it like this:
myapp 2> /dev/null
See you next time…