Mirroring console output to a file
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Mirroring console output to a file means you want to see output live on screen and save it at the same time. The core tool for that job in Unix-like shells is tee, while Windows PowerShell uses Tee-Object. The main detail people miss is whether they need only standard output, or both standard output and standard error.
Basic Mirroring With tee
On Unix and Linux, the simplest pattern is:
This sends standard output to both the terminal and output.log.
For example:
You see the directory listing in the terminal, and the same lines are written to files.log.
By default, tee overwrites the target file. To append instead, use -a.
That is the right choice for long-running logs or repeated runs.
Mirror Standard Error Too
Many useful diagnostics are written to standard error, not standard output. If you only pipe stdout, error messages may appear on screen but never reach the file.
To capture both streams in bash or zsh:
2>&1 redirects stderr to stdout before the pipe, so both streams flow into tee.
This is the version you usually want for builds, tests, and deployment scripts.
Keep Exit Codes in Mind
A subtle problem with pipes is exit status. In many shells, the pipeline exit code defaults to the last command, which is tee, not the original command.
For bash, you can preserve the original command status with pipefail:
Without pipefail, a failed build command may look successful because tee itself succeeded.
Mirroring in PowerShell
PowerShell provides Tee-Object.
To append:
PowerShell stream handling is richer than classic shell redirection, so exact behavior depends on which stream the command writes to. For many everyday commands, Tee-Object is enough.
Save Output From a Script
In shell scripts, tee is a good way to create a durable build or deployment log.
This gives you a live console view and a log file for later inspection.
If you want the whole script to mirror output, you can redirect once near the top:
After that, all following stdout and stderr from the script go through tee.
Mirroring From a Program
Sometimes you want the program itself to write to both console and file without relying on shell tools. In Python, the logging module handles this well.
This is better than shell redirection when you want structured logs and control over formatting.
Use the Right Tool for the Job
Use shell-level mirroring when:
- you are wrapping an existing command
- you want a quick run log
- you do not control the program source
Use application-level logging when:
- you own the code
- you need timestamps, log levels, or rotation
- you want predictable behavior across environments
These approaches complement each other rather than competing.
Performance and File Growth
Mirroring is simple, but logs can grow very quickly. For repeated or long-running jobs:
- append only when that is truly what you want
- rotate or archive logs regularly
- avoid writing multiple noisy processes to the same file unless ordering is not important
If many processes write to the same file concurrently, the resulting log can be interleaved and harder to read.
Common Pitfalls
Using tee without redirecting stderr, then wondering why error messages are missing from the log.
Forgetting that tee overwrites by default unless you add -a.
Losing the original command's exit status because the pipeline returns the tee status.
Using shell redirection when the application really needs structured logging in code.
Letting log files grow without rotation or cleanup.
Summary
- On Unix-like systems,
teemirrors console output to a file. - Use
2>&1 | tee file.logwhen you need both stdout and stderr. - Use
tee -ato append instead of overwrite. - In PowerShell, use
Tee-Objectfor the same pattern. - Prefer application-level logging when you need structured, long-term log management.

