tee on Linux: Pipe to Screen and Log Files (tee linux)

tee linux - custom-tee2-featured.png

Query tee linux is operators wanting visible output and a log file. See also tee command basics and cron wrappers.

Proxmox admins: log installs beside apt troubleshooting.

Stdout and file

Default overwrites; -a appends for nightly jobs.

bashtee
./deploy.sh 2>&1 | tee -a deploy-$(date +%F).log
tee -a
log

Sudo writes

Pipe to sudo tee when editing protected files from heredocs.

bashsudo
grep -v '^#' /etc/ssh/sshd_config | sudo tee /tmp/sshd.clean
sudo tee
etc

Exit code caveat

Pipeline status follows the last command — use set -o pipefail.

bashpipefail
set -o pipefail
false | tee /tmp/out; echo exit=$?
pipefail
bash

With systemd

Prefer journal for services; tee for one-off maintenance scripts.

TipRotate large tee logs with logrotate or compress with gzip.
cron
scripts

Quick Reference

  • cmd | tee file
  • Append: tee -a
  • Enable set -o pipefail in scripts

Related tutorials

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