đźšż Command: tee (The T-Splitter)
The tee command is named after a T-fitting used in plumbing. It takes the output of a command and splits it: it saves it to a file and simultaneously displays it in your terminal.
1. The "Why"
As a developer and power user, you often run commands where you need to see the result now but also keep a record for later.
- Logging while Debugging: Run your application and see the errors in real-time while saving them to
debug.log. - Sudo Redirection: This is the most famous use case. Standard redirection (
>) often fails withsudobecause the shell—not the command—handles the file writing.teesolves this. - System Monitoring: Watch your CPU temperatures and pipe them to a file for later analysis.
2. Basic Syntax
command | tee filename
3. Practical Examples for Your Workflow
A. The "Sudo Trick" (Writing to Protected Files)
If you try to edit a system file like this, it will fail:
sudo echo "nameserver 8.8.8.8" > /etc/resolv.conf (The > happens as your normal user).
The fix:
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
B. Appending Instead of Overwriting (-a)
By default, tee overwrites the file. Use the -a flag to add to the end of it:
echo "New log entry" | tee -a activity.log
C. Splitting to Multiple Files
You can save the output to as many files as you want:
ls -la | tee file1.txt file2.txt file3.txt
D. Silencing the Terminal
If you want to use tee for its sudo powers but don't actually want to see the output in the terminal, redirect the output to /dev/null:
echo "config_value=true" | sudo tee /etc/myapp.conf > /dev/null
4. Advanced: Pipe Interception
You can use tee in the middle of a complex "pipe chain" to inspect data at a specific stage without stopping the process.
cat data.txt | tee raw_backup.txt | grep "ERROR" | tee errors_only.txt | wc -l
This command saves the original data, saves the filtered errors, and finally counts how many errors there were—all in one line.
5. Pro-Tips
- Arch Linux Context: You’ll frequently use
teewhen following Arch Wiki instructions for configuring hardware, such as forcing a specific power state for your GPU by writing to/sys/files. - Security: Be careful using
teewith commands that output passwords or API keys, as they will be saved in plain text to your logs. - Interactivity:
teehandles binary data just as well as text, so you can use it to create a copy of a downloaded.tar.gzfile while it's being piped to another tool.
6. Summary Reference
| Goal | Command |
|---|---|
| Save and View | `command |
| Save and View (Append) | `command |
| Write to Root file | `echo "text" |
| Save to multiple files | `command |