tee Command on Linux: Write Output to File and Terminal

tee command linux - custom-tee-featured.png

tee on Linux solves a common problem: you want to see command output live and save it for a ticket or post-mortem. Unlike shell redirection alone, tee copies the stream to one or more files while still printing to the terminal.

See: cron logging wrapper,
journalctl,
tail -f.

Basic tee

Overwrite the log file each run unless you pass -a to append.

bashsave and show
./deploy.sh 2>&1 | tee /var/log/deploy.log
tee
log file

Append mode

Long-running maintenance jobs should append with timestamps in the script header.

bashappend
apt upgrade -y 2>&1 | tee -a /var/log/apt-upgrade.log
tee -a
append

sudo tee

Redirecting to /etc with > fails under sudo — pipe through tee instead.

bashwrite config
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-forward.conf
sudo tee
/etc

Multiple files

tee can write to several destinations — handy for local log plus NFS mirror.

WarningLog files under /var/log may need root ownership — create them with correct permissions first.
tee file1
file2

Quick Reference

  • cmd | tee file.log
  • Append: tee -a
  • Protected paths: | sudo tee path

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.