Master the Linux tee Command: Write, Append, and Redirect Output Efficiently
This guide explains the Linux tee utility, covering its purpose, installation verification, basic syntax, useful options, and seven practical examples that demonstrate writing to files, appending data, handling multiple outputs, ignoring interrupts, and combining tee with sudo for privileged writes.
What Is the tee Command?
The tee command reads from standard input and writes the data simultaneously to standard output (your screen) and one or more files, making it handy for chaining commands while still preserving the original output.
Installation Check
teeis part of GNU Coreutils and is pre‑installed on virtually all Linux distributions. Verify its presence with: which tee Typical output: /usr/bin/tee Check the version with:
tee --versionBasic Syntax
The general form is: tee [options]... [file]... Common options include: -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 still displaying it
ping yahoo.com | tee pingLinuxMi.txtThis captures the ping results in pingLinuxMi.txt and also prints them to the terminal.
2. Append data to an existing file
ping 192.168.174.170 | tee -a pingLinuxMi.txtThe -a flag ensures new output is added to pingLinuxMi.txt rather than replacing its contents.
3. Write to multiple files at once
[your_command] | tee file1 file2 file3 file4All specified files receive the same output.
4. 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 while tee is running
[command] | tee -i output.txtThe -i option prevents Ctrl+C (SIGINT) from terminating the tee process.
6. Suppress tee output by redirecting to /dev/null
[command] | tee output_file_name >/dev/nullThe command’s output is saved to output_file_name but not shown on the screen.
7. Use tee with sudo to write to privileged files
echo "300" | sudo tee /etc/linuxmi.confRunning tee under sudo allows writing to files owned by the root user.
Conclusion
By mastering tee, you can efficiently capture, append, and redirect command output in a variety of scenarios, making your shell workflows more flexible and powerful.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
