Master the Linux tee Command: Real‑World Examples and Tips
This guide explains what the Linux tee command does, how it’s installed, its basic syntax and options, and provides practical examples—including writing output to files, appending data, handling multiple files, piping to other commands, ignoring interrupts, hiding output, and using sudo—so readers can efficiently capture and process command‑line output.
When working in a Linux terminal you may need to write data to a file while still processing its output; the tee utility makes this possible.
What is the tee command?
The tee command reads from standard input and writes to both standard output (your screen) and one or more files, similar to a T‑shaped pipe used by plumbers.
Installation
teeis included in GNU Coreutils, so it is pre‑installed on most Linux distributions. Verify its presence with: which tee Check the version with:
tee --versionBasic syntax
The general form is: tee [options]... [file]... Common options: -a: Append to the file instead of overwriting. --version: Show the installed version. --help: List all available options.
Practical examples
1. Write ping output to a file while displaying it
ping yahoo.com | tee pingLinuxMi.txtThis captures the ping results in pingLinuxMi.txt while still showing them on the terminal.
2. Append data to a file
ping 192.168.174.170 | tee -a pingLinuxMi.txt3. Write to multiple files simultaneously
[your command] | tee file1 file2 file34. Pipe tee output to another command
ls ~ | tee linuxmi.txt | wc -lThis lists files in the home directory, saves the list to linuxmi.txt, and counts the number of entries.
5. Ignore interrupt signals
[command] | tee -i output_fileThe -i option prevents Ctrl+C (SIGINT) from terminating the pipeline.
6. Hide tee output
[command] | tee output_file > /dev/nullThis writes the output to output_file while discarding it from the screen.
7. Use tee with sudo for privileged files
echo "300" | sudo tee /etc/linuxmi.confRedirects data to a file owned by the root user without permission errors.
Conclusion
With these examples you should now understand how to use tee for everyday tasks, from simple logging to complex pipelines.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
