- Posted on
- • Advanced
Understanding and Using `tee` for Capturing Output
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
The tee command in Unix-like operating systems is a powerful utility for capturing and duplicating command output. It allows you to both display the output of a command on the terminal and simultaneously write it to a file. Here's a detailed guide to understanding and using tee.
Basic Syntax
command | tee [options] [file...]
command: The command whose output you want to capture.|: A pipe that passes the output ofcommandtotee.tee: The command that reads from standard input and writes to standard output and file(s).file...: One or more files where the output will be saved.
How tee Works
- Standard Output Display:
teesends the output to the terminal (standard output). - File Saving: Simultaneously, it writes the same output to one or more specified files.
Common Use Cases
1. Log Output While Viewing
To log output to a file while still seeing it in real time on the terminal:
ls -l | tee output.txt
2. Append to a File
By default, tee overwrites the file. Use the -a option to append instead:
ls -l | tee -a output.txt
3. Capture and Process Output
You can use tee in pipelines to split the output for multiple processing streams:
cat file.txt | tee intermediate.txt | grep "pattern"
Here:
The content of
file.txtis saved inintermediate.txt.The
grepcommand processes the same output.
4. Save Multiple Outputs
Save output to multiple files simultaneously:
ls -l | tee file1.txt file2.txt
5. Debugging Scripts
Log the output of a script while running it interactively:
./script.sh | tee debug.log
Common Options
-a: Append the output to the file(s) instead of overwriting.-i: Ignore interrupt signals, ensuringteecontinues writing even if interrupted.
Practical Examples
Redirect Standard Error (stderr)
To capture both standard output (stdout) and standard error (stderr):
command 2>&1 | tee output.txt
Save Output for Multiple Commands
Using tee in a multi-stage pipeline:
command | tee temp.txt | another_command
Interactive Use with Sudo
When saving files requiring elevated permissions:
command | sudo tee /path/to/protected_file
Tips
Use
teefor monitoring logs in real-time during processes like backups or deployments.Be cautious when overwriting files, as
teeoverwrites without confirmation unless-ais used.
The tee command enhances command-line workflows by providing a simple way to capture, inspect, and process output without disrupting its flow.
Further Reading
For readers interested in extending their knowledge about the tee command and related command-line utilities in Linux, the following resources can be highly beneficial:
Linux
teeCommand Summary
A concise summary of theteecommand and its options: https://www.geeksforgeeks.org/tee-command-linux-example/Understanding Linux Pipes and Redirection
An in-depth guide to using pipes and redirection, critical for masteringtee: https://linuxize.com/post/bash-pipes/Advanced Bash-Scripting Guide
A comprehensive tutorial that includes how to useteein scripting: https://tldp.org/LDP/abs/html/Using
teewith sudo Command
Specifics on usingteein conjunction with sudo for permission management: https://www.cyberciti.biz/faq/linux-unix-bsd-appleosx-tee-command/Practical Examples of
teeCommand in Linux
Provides practical uses and examples to harnessteeeffectively: https://www.tecmint.com/tee-command-usage-in-linux/
Each of these resources delves into specific aspects and applications of tee, complementing the foundational knowledge provided and enhancing both the practical and theoretical understanding of this versatile command.