TIL: Using tee To Split Command Output to File and Terminal
When a build script runs you want to see the output in real time and save it to a file in case something fails. tee does both.
npm run build | tee build.log
stdout goes to the terminal and into build.log at the same time. When the build fails 200 lines up, you have the log file to scroll through instead of scrolling your terminal buffer.
npm run build 2>&1 | tee build.log
Redirect stderr to stdout first so tee captures errors too.
Does tee overwrite or append?
tee overwrites by default. Use tee -a to append to an existing file.
Can I write to multiple files?
command | tee file1.log file2.log
Recovers the output and runs scripts repeatedly. Useful for CI debugging where the terminal output vanishes when the job ends.