Print STDOUT/STDERR and write them to a file in Bash?

Naftuli Kay picture Naftuli Kay · Jan 19, 2011 · Viewed 7.4k times · Source

Is there a way to have Bash redirect STDOUT/STDERR to a file yet still print them out to the terminal as well?

Answer

SiegeX picture SiegeX · Jan 19, 2011

This will redirect both STDOUT and STDERR to the same file:

some_command 2>&1 | tee file.log

Example

$ touch foo; ls foo asfdsafsadf 2>&1 | tee file.log
ls: asfdsafsadf: No such file or directory
foo
$ cat file.log
ls: asfdsafsadf: No such file or directory
foo