Bash Logs IO Redirection – How to Redirect stderr and stdout to Different Files and Also Display in Terminal

bashio-redirectionlogs

I want to see the output of a command in the terminal as if there was no redirection.
Also, stderr needs to be redirected to err.log and stdout needs to be redirected to stdout.log.

It would be nice to also have the exact copy of what is shown in terminal, i.e. errors printed as and when it occurs, in a separate file: stdouterr.log.

Best Answer

Use the tee command as follows:

(cmd | tee stdout.log) 3>&1 1>&2 2>&3 | tee stderr.log

3>&1 1>&2 2>&3 is how you swap stderr and stdout, because tee can only accept stdout.

Take a look at Unix tee command for more advanced redirections using tee.

Related Question