tee Command on Linux: Write Output to File and Terminal (Examples)

tee linux - custom-tee51-featured.png

tee linux shows up when admins need logs on screen and disk — especially with sudo tee for config files. See also tee in shell scripts.

Basic pipe to tee

Split stdout to terminal and file.

bashtee
echo 'deploy ok' | tee /var/log/deploy.log
pipe
basic

Append with -a

Do not truncate — use -a for growing logs.

bashappend
date | tee -a /var/log/deploy.log
-a
append

sudo tee

Redirects do not work with sudo — tee writes the file as root.

bashsudo tee
grep -v '^#' my.conf | sudo tee /etc/app/app.conf > /dev/null
sudo
etc

Capture stderr too

Use 2>&1 before the pipe.

bashboth
myscript.sh 2>&1 | tee run.log
2>&1″ /><figcaption>stderr</figcaption></figure>
<h3>Quick Reference</h3>
<ul>
<li>Scripts: <a href=tee logging patterns
  • tee -a for cron output
  • Related tutorials

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