How to redirect stdout/stderr to file?

How to redirect stdout/stderr to file?

  • 2020-2-28

One common pattern that you perhaps saw before is using 2>&1, which means redirect stderr to stdout. Combined with it, we can use > or >> to redirect both stdout/stderr to some file.

command 2>&1 > file # or commmand 2>&1 >> file

Note: the different between > and >> is that > will overwrite the previous content of the file and >> will append the text at the bottom of the previous file.


The bash 4 gives you another option to finish the same task as we described before:

command &> file

The above command will redirect both stdout and stderr to a file.

Writes output both to the screen and a file

Sometimes we'd like to write the stdout/stderr to some file and keep the output on the screen at the time. What should we do in this situation?

One useful command that helps us to do this task is the tee. The tee command writes the output to the screen (stdout/stderr) and to the file in the meanwhile. For example:

date | tee file

The above command will print the current date on the scrren and save it the file.

One common need that we want to achieve is print the original output of the command to the screen and do some operations (for instance, we pass it as input to the sed to do the substitution).

The below example will writes the output of the first command and remove the color codes (eg. [39m) using sed and then save the substituted one to the file:

command 2>&1 | tee /dev/stderr | gsed -ur 's/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g;' &> ~/.log

PS: The native sed command in the BSD systems (eg. MacOS) do not support -u option, which will force the output to be line buffered. A more detailed explanation of this option is appended below:

sed -u: Force output to be line buffered, printing each line as it becomes available. By default, output is line buffered when standard output is a terminal and block buffered otherwise. See setbuf(3) for a more detailed explanation.

PSS: -r option of sed is aimed to use extended regular expression syntax.