Skip to content
Log In Sign Up info@muvcloud.com

The Basic Command

df -h | tee disk-report.txt

The output of df -h appears on screen as usual and is also saved to disk-report.txt. tee replaces the file if it exists. The name comes from the T-shaped pipe fitting: one stream in, two streams out. On Ubuntu 26.04 tee comes from the Rust rewrite of the core commands; the options in this guide work the same there.

Common Tasks

TaskCommand
Save and showcmd | tee out.txt
Appendcmd | tee -a out.txt
Two filescmd | tee a.txt b.txt
Root-owned filecmd | sudo tee file
Hide the screen copycmd | tee file > /dev/null
Include errorscmd 2>&1 | tee out.txt

Append Instead of Overwrite

date | tee -a checks.log
df -h / | tee -a checks.log

-a adds to the end of the file. Use it for any file you build up over time.

Several Files and the Middle of a Pipeline

uname -a | tee report.txt /tmp/report-copy.txt
ls -l /etc | tee full-list.txt | grep nginx

In the second line tee saves the full list and passes it on unchanged, so grep still filters what you see.

Write a Root-Owned File with sudo tee

A redirection such as > /etc/file is carried out by your own shell, as your own user, before sudo runs. That is why putting sudo in front of echo still ends in "permission denied". tee runs as its own program, so sudo can give it root rights:

echo "vm.swappiness = 10" | sudo tee /etc/sysctl.d/99-swappiness.conf

Before you press Enter on a line like this, check whether you need -a: without it, tee replaces the whole file. For a file that already has content, such as /etc/hosts, make a copy first and append:

sudo cp /etc/hosts /etc/hosts.bak
echo "203.0.113.10 db.internal" | sudo tee -a /etc/hosts

Several lines at once work with a here document. Adding > /dev/null keeps the copy off the screen:

sudo tee /etc/sysctl.d/90-network.conf > /dev/null <<'EOF'
net.core.somaxconn = 8192
net.ipv4.tcp_fin_timeout = 30
EOF
sudo sysctl --system

The format of the hosts file is covered in how to edit the hosts file in Linux.

Capture Errors Too

tee only receives standard output. Error messages go to standard error, so they appear on screen but are missing from the file. Redirect them into the pipe with 2>&1 before the pipe sign:

./deploy.sh 2>&1 | tee deploy.log
./backup.sh 2>&1 | tee -a ~/backup-runs.log

The Exit Status in a Pipeline

A pipeline returns the exit status of its last command, and that is tee, which almost always succeeds. A failed deployment piped into tee therefore looks like a success to &&, if, or cron. In Bash, turn on pipefail or read the status of the first command from PIPESTATUS:

#!/bin/bash
set -o pipefail
./deploy.sh 2>&1 | tee deploy.log
echo "deploy exit code: $?"

./backup.sh | tee backup.log
echo "backup exit code: ${PIPESTATUS[0]}"

More on scripts is in how to write a Bash script.

Common Problems

  • Permission denied with sudo echo: the redirect ran as your user. Use sudo tee.
  • File emptied: tee ran without -a. Restore it from the copy you made.
  • Output arrives in bursts: many programs buffer output when they write to a pipe. For programs that use the C library's standard buffering, stdbuf -oL in front of the command makes them pass each line on at once; programs that set their own buffering ignore it.
  • Reading and writing the same file: a pipeline that reads a file and tees into that same file can empty it before it is read. Write to a new file and move it into place.

To follow a log that tee writes, use the tail command in a second terminal.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations