What it does
tee sits in a pipe like a T-piece in a water pipe: data goes through, and a copy goes into files. You see the output and keep it.
A very common use: write to a file that only root may change, from a normal user, with echo ... | sudo tee FILE. The redirect > would run as your user and fail; tee runs with sudo.
How to write it
tee [OPTION]... [FILE]...Options 6
Most used
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1See the output and save it
dnf check-update | tee updates.txtYou watch the list and keep a copy for your change ticket.
2Write a root-owned file from a normal user
sudo echo ... > filefails because the redirect is not done by sudo.3Add a line to a root-owned file
-aappends. Without it the file would be replaced.4Hide tee's copy on the screen
Only the file gets the text.
5Write several files at once
One command, two copies.
6Log a long script and still watch it
./upgrade.sh 2>&1 | tee -a /var/tmp/upgrade-$(date +%F).log2>&1sends errors into the pipe too, so the log is complete.7Save the middle of a pipe
journalctl -u sshd --since today | tee ssh-today.log | grep -c "Failed password"The full log is saved; the screen shows only the count of failed logins.
8Write a heredoc to a protected file
sudo tee /etc/yum.repos.d/local.repo > /dev/null <<'EOF' [local-baseos] name=Local BaseOS baseurl=file:///repo/BaseOS gpgcheck=0 EOFA clean way to create config files in scripts that run as a normal user with sudo.
9Keep running when you press Ctrl+C
ping server2 | tee -i ping.logWith -i, tee is not stopped by Ctrl+C, so the log gets the last lines.
10Record a kubectl apply for the change log
kubectl apply -f deploy.yaml 2>&1 | tee -a changes.logEvery change is visible now and stored for later.
11Count and save matches at the same time
The number on the screen, the lines in the file.
Nothing matches this filter.
Watch out
- Without
-a, tee overwrites the file. - In
sudo tee, only tee runs as root; the command before the pipe runs as you.
Tips to remember
- tee = T-piece: one input, two outputs (screen + file).
| sudo tee FILE= write as root.| sudo tee -a FILE= append as root.