The Bash shell is one of the best parts of the user environment on a Linux system. An essential shell function is the ability to run a command and redirect the output to a file.
Programmers will be familiar with command output being split into two parts, the standard output (stdout), and the standard error (stderr) streams. In both Linux and Windows command-line shells, these output streams are also referred to by number: 1 for stdout and 2 for stderr. The default output stream is “1” and is implied if you don’t specify a number.
You can redirect each stream to a separate file, or you can combine both into a single stream and redirect to only one file. I keep needing to look up the syntax for combine and redirect because in bash only one order will work. Here are some examples:
# stdout (1) to one file and stderr (2) to another somecommand >out.txt 2>err.txt # stderr (2) goes to same place as stdout (1) somecommand >both.txt 2>&1
If you would like to see output on the screen and save the same output in a file, check out the standard “tee” command. Cheers! 🙂