tee Command — Writing Output to Both Screen and File¶
The
teecommand reads data from standard input and writes it simultaneously to the terminal and one or more files. It is an essential Linux utility for logging command output, debugging shell scripts, monitoring automation, and building CI/CD pipelines.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
teecommand - Save command output while displaying it
- Append output to files
- Write to multiple files
- Use
teein shell pipelines - Log automation output
- Apply
teein DevOps and CI/CD workflows
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 Lessons 1–13
Why Learn tee?¶
Normally:
Output goes to the file.
Nothing appears on the screen.
Or:
Output appears on the screen.
Nothing is saved.
What if you need both?
Now you can:
- View the output
- Save it to a file
At the same time.
What is tee?¶
The tee command copies its input to:
- Standard Output (Terminal)
- One or more files
Syntax:
Think of it as a T-junction in a pipe.
Basic Usage¶
Output on screen:
File:
Output:
Save Command Output¶
You will see the directory listing while also saving it.
Append Instead of Overwrite¶
Normally:
overwrites the file.
Append instead:
View:
Output:
Write to Multiple Files¶
All files receive the same output.
Use with grep¶
Save matching lines.
Display:
The same output is saved to errors.log.
Use with sort¶
Use with wc¶
Workflow:
Use with find¶
Logging Script Output¶
You can monitor progress in real time while keeping a log for later review.
Capture Both Standard Output and Errors¶
Normally, tee captures only standard output.
To capture both:
Example:
Output on screen:
The same content is saved to result.log.
Ignore Interrupt Signals¶
Useful for long-running processes.
The -i option ignores interrupt signals (Ctrl+C) sent to tee.
Common tee Options¶
| Option | Description |
|---|---|
-a | Append instead of overwrite |
-i | Ignore interrupt signals |
Combining with Other Commands¶
Monitor disk usage.
Save running processes.
Save Kubernetes Pods.
Save Docker containers.
Save environment variables.
Real Production Examples¶
Save deployment logs.
Save Terraform output.
Save Ansible execution.
Save Helm installation.
Save Docker build logs.
Production Perspective¶
The tee command is one of the most frequently used utilities in:
- CI/CD pipelines
- DevOps automation
- System administration
- Deployment scripts
- Infrastructure provisioning
- Production troubleshooting
It allows engineers to monitor commands while simultaneously creating logs for auditing and debugging.
Hands-on Lab¶
Task 1¶
Display and save text.
Task 2¶
Append another line.
Task 3¶
Display the file.
Task 4¶
Save directory listing.
Task 5¶
Save process list.
Task 6¶
Write to multiple files.
Task 7¶
Capture both output and errors.
Task 8¶
Count files while saving the list.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
tee file | Save output | Reports |
tee -a | Append logs | Automation |
tee file1 file2 | Multiple outputs | Backups |
2>&1 \| tee | Capture stdout + stderr | Troubleshooting |
Production Troubleshooting Scenario¶
Scenario
A DevOps engineer is deploying an application to Kubernetes.
Tasks:
- Watch deployment progress live.
- Save deployment logs.
- Capture errors.
- Share the log with the support team.
Solution:
Benefits:
- Live monitoring
- Permanent log file
- Easier troubleshooting
- Audit trail
Mini Challenge¶
Perform the following:
- Save the output of
df -hwhile displaying it. - Save the output of
ps -ef. - Append a custom message to an existing log.
- Capture errors from an invalid command.
- Save the list of shell scripts in the current directory.
- Write the same output to two different files.
Best Practices¶
- Use
teein deployment and automation scripts. - Use
-afor log files that should be preserved. - Capture both stdout and stderr using
2>&1. - Use descriptive log file names.
- Rotate or archive large log files regularly.
Performance Tip¶
For long-running commands that produce continuous output:
This allows real-time monitoring while creating a persistent copy for later analysis.
Common Mistakes¶
❌ Forgetting -a.
✅ Without -a, the file is overwritten.
Incorrect:
Correct:
❌ Assuming tee captures error output automatically.
✅ Incorrect:
Correct:
❌ Using tee without considering log file growth.
✅ Regularly rotate or clean up logs in long-running systems.
Interview Questions¶
Beginner¶
- What is the purpose of the
teecommand? - How do you append output to a file?
- How do you write to multiple files?
- Why is
teeuseful in pipelines?
Intermediate¶
- Explain the difference between
>andtee. - How do you capture both standard output and standard error?
- What does the
-ioption do? - Why is
teecommonly used in shell scripts?
Architect Level¶
- How would you log a Kubernetes deployment while monitoring it live?
- Why is
teevaluable in CI/CD pipelines? - How would you design a deployment script that logs every step without hiding console output?
Summary¶
In this lesson, you learned:
- Displaying and saving output simultaneously
- Appending to log files
- Writing to multiple files
- Capturing errors
- Using
teein shell pipelines - Real-world DevOps and automation use cases
The tee command is a simple yet powerful utility that bridges the gap between visibility and logging. It is widely used in Linux administration, automation, CI/CD, and production troubleshooting.
Key Takeaways¶
teewrites output to both the terminal and files.- Use
-ato append instead of overwrite. - Use
2>&1 | teeto capture both standard output and errors. teeintegrates naturally with pipelines.- It is a standard tool for logging deployments, builds, and automation tasks.
What's Next?¶
xargs Command — Building Powerful Command Pipelines in Linux
In the next lesson, you'll learn:
- Passing input as command arguments
- Running commands on multiple files
- Combining
findwithxargs - Parallel execution
- Production automation examples