The "tee" Command in Linux: A Hidden Gem for Smart Data Handling
In Linux, there are many powerful commands that make working with the command line easier. One such underrated tool is "tee"—a simple yet extremely useful command that helps store and view command output simultaneously. If you've ever needed to save output to a file but still see it on the screen while running a command, "tee" is the perfect solution. Let’s break down what it does, why it matters, and how you can use it like a pro. 1. What Is the "tee" Command? 2. Why Is "tee" Important? 3. How to Use "tee" in Linux Basic Usage: Viewing and Saving Output Simultaneously Appending Data Instead of Overwriting Using "tee" with Multiple Commands 4. Real-World Use Cases of "tee" Use Case 1: Logging System Commands in Real Time Use Case 2: Saving Error Messages While Running a Script Use Case 3: Viewing Updates While Writing to a Log File Final Thoughts 1. What Is the "tee" Command? The "tee" command reads output from another command and does two things at the same time: Displays the output on your screen (stdout). Saves the same output to a file. Normally, when you run a command, the results show up in the terminal but don’t get saved anywhere. With "tee," you can store the output without losing visibility—which is great for troubleshooting, logging, and automation. Think of it like a pipe that splits water—some water (output) continues to flow to the screen, while some gets collected in a container (file). 2. Why Is "tee" Important? Prevents loss of command output – No need to re-run a command just to save results. Helps debug issues – Easily monitor output while logging data for later analysis. Automates logging – Store important information from long-running commands. Supports multi-process workflows – Send output to multiple programs at once. 3. How to Use "tee" in Linux Basic Usage: Viewing and Saving Output Simultaneously Let’s say you want to list files in a directory and save the output for later: ls -l | tee file_list.txt What happens here? You see the list of files on your screen. The same list gets saved in file_list.txt. Appending Data Instead of Overwriting By default, "tee" overwrites the file. To append data instead of replacing it, use -a: ls -l | tee -a file_list.txt This keeps old data in the file and adds new output to the end. Using "tee" with Multiple Commands You can use "tee" to send output to multiple files: df -h | tee disk_usage.txt | tee storage_report.txt Now, the disk usage report gets saved in both files while still appearing on the screen. 4. Real-World Use Cases of "tee" Use Case 1: Logging System Commands in Real Time Imagine you’re checking CPU usage but want to keep a record of the output: top -n 1 | tee cpu_log.txt This captures a snapshot of your system performance for future analysis. Use Case 2: Saving Error Messages While Running a Script If a script fails, capturing error messages helps with debugging: ./backup_script.sh 2>&1 | tee error_log.txt 2>&1 ensures both standard and error messages are saved. You see the errors in real-time while storing them in error_log.txt. Use Case 3: Viewing Updates While Writing to a Log File When running long system updates, tracking progress helps: sudo apt update | tee update_log.txt This ensures you see what’s happening while logging the details. Final Thoughts The "tee" command is a must-know tool for Linux users, whether you’re a beginner or a seasoned pro. It keeps your workflow smooth, ensures important output is logged, and saves time by preventing the need to re-run commands.

In Linux, there are many powerful commands that make working with the command line easier. One such underrated tool is "tee"—a simple yet extremely useful command that helps store and view command output simultaneously.
If you've ever needed to save output to a file but still see it on the screen while running a command, "tee" is the perfect solution. Let’s break down what it does, why it matters, and how you can use it like a pro.
- 1. What Is the "tee" Command?
- 2. Why Is "tee" Important?
-
3. How to Use "tee" in Linux
- Basic Usage: Viewing and Saving Output Simultaneously
- Appending Data Instead of Overwriting
- Using "tee" with Multiple Commands
-
4. Real-World Use Cases of "tee"
- Use Case 1: Logging System Commands in Real Time
- Use Case 2: Saving Error Messages While Running a Script
- Use Case 3: Viewing Updates While Writing to a Log File
- Final Thoughts
1. What Is the "tee" Command?
The "tee" command reads output from another command and does two things at the same time:
- Displays the output on your screen (stdout).
- Saves the same output to a file.
Normally, when you run a command, the results show up in the terminal but don’t get saved anywhere. With "tee," you can store the output without losing visibility—which is great for troubleshooting, logging, and automation.
Think of it like a pipe that splits water—some water (output) continues to flow to the screen, while some gets collected in a container (file).
2. Why Is "tee" Important?
- Prevents loss of command output – No need to re-run a command just to save results.
- Helps debug issues – Easily monitor output while logging data for later analysis.
- Automates logging – Store important information from long-running commands.
- Supports multi-process workflows – Send output to multiple programs at once.
3. How to Use "tee" in Linux
Basic Usage: Viewing and Saving Output Simultaneously
Let’s say you want to list files in a directory and save the output for later:
ls -l | tee file_list.txt
What happens here?
- You see the list of files on your screen.
- The same list gets saved in
file_list.txt
.
Appending Data Instead of Overwriting
By default, "tee" overwrites the file. To append data instead of replacing it, use -a
:
ls -l | tee -a file_list.txt
This keeps old data in the file and adds new output to the end.
Using "tee" with Multiple Commands
You can use "tee" to send output to multiple files:
df -h | tee disk_usage.txt | tee storage_report.txt
Now, the disk usage report gets saved in both files while still appearing on the screen.
4. Real-World Use Cases of "tee"
Use Case 1: Logging System Commands in Real Time
Imagine you’re checking CPU usage but want to keep a record of the output:
top -n 1 | tee cpu_log.txt
This captures a snapshot of your system performance for future analysis.
Use Case 2: Saving Error Messages While Running a Script
If a script fails, capturing error messages helps with debugging:
./backup_script.sh 2>&1 | tee error_log.txt
-
2>&1
ensures both standard and error messages are saved. - You see the errors in real-time while storing them in
error_log.txt
.
Use Case 3: Viewing Updates While Writing to a Log File
When running long system updates, tracking progress helps:
sudo apt update | tee update_log.txt
This ensures you see what’s happening while logging the details.
Final Thoughts
The "tee" command is a must-know tool for Linux users, whether you’re a beginner or a seasoned pro. It keeps your workflow smooth, ensures important output is logged, and saves time by preventing the need to re-run commands.