Build a Better Process

Linux tee Command Summary with Examples

Linux tee allows you to route to both standard output and a file.
  1. Purpose - Learn what tee is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with tee.
  4. Script - Add tee to our script and run it.
  5. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux tee, 91 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / tee-command.html

An ad-free and cookie-free website.

Examples of the Linux tee Command


Learn to route command output to two locations at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux tee Command Summary with Examples (3:46)

Video Script

The Command and Why You Need It

Our ninety-first word, or command to memorize is tee from our category Workflow.

tee allows you to route to both standard output and a file.

Common Linux tee Options
-options description
--help Print help screen
-a, --append Append to file instead of overwrite

Recall from videos (tutorials) #87 to #90, we're building a script to demonstrate workflow, well now we'll cover the tee command and add it to a script.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The tee command has 4 options and the argument is a filename followed by a second pipe. Think of it as if the second pipe can be for additional processing by another command. And we'll see that in a second.

Like most commands, help is available with double-dash --help, -a appends, rather than overwrites output to a file.

So why is tee an important command? Well, we can eliminate the need to write two separate commands to route output to two different places. And now you know how to do that.


Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's ls all of the text files in our working directory.

$ ls *.txt video05.txt video23.txt video33.txt video38b.txt video54.txt video67.txt video71.txt video11.txt video24.txt video34.txt video38c.txt video55.txt video68a.txt video73a.txt video16.txt video26.txt video35.txt video40.txt video56.txt video68b.txt video75.txt video19.txt video27.txt video36.txt video44.txt video57.txt video69a.txt video76.txt video20.txt video31.txt video37.txt video48a.txt video64.txt video69b.txt video85a.txt video21.txt video32.txt video38a.txt video48b.txt video65.txt video70.txt video85b.txt

And there are quite a few. You could pipe, or redirect this output to another command, like the word count for lines using wc -l.

$ ls *.txt | wc -l 42

And get 42 files. So you've routed to another command.

Next, what if you wanted to stop after the first listing, write to a file, and then keep processing with another command? Hmmm.

So let's do it in our script, at the bin directory we have a script. It's called funscript.

$ nano ~/bin/funscript

(Below is the screen from within nano.)

GNU nano 2.2.6 File: /home/factorpad/bin/funscript #!/bin/bash # The shebang gives the absolute path to the executable program # To see if it points to the right executable try: which bash # This is our first script called funscript # From video 87, print text and the current date/time echo -e "\nThe current date and time:" date # From video 88, we measure the time to run the script sleep 2s # From video 89, we take input from the keyboard echo -e "What is your name? \c" read yourname echo "Thank you $yourname" # From video 90, report files and lines since last archive echo "Lines and files since your last archive:" find . -type f -cnewer "video73.tar" | xargs wc -l # From video 91, use tee to print files and show last five echo "You've been busy. See video91.txt for a list of files" echo "Here are your latest five files. Nice work!" ls *.txt | tee video91.txt | tail -5 [ Read 29 lines ] ^G Get Help ^0 WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

(Here we added lines about video 91 and below.)

Here I added a comment, text to explain the output, followed by the ls *.txt from earlier, and then tee it to a file, and then report out the last 5 lines.

Okay, please pause if you'd like to catch up on your end and then we'll try it out.

(Hit Ctrl-x to leave nano and y to confirm saving.)

And last, explicitly run with this, in the bin directory, then funscript.

$ ~/bin/funscript The current date and time: Mon Nov 15 17:22:14 PST 2016 What is your name? paul Thank you paul Lines and files since your last archive: 0 ./video79.script 5 ./video85.txt 0 ./video75.txt 0 ./video76.txt 61 ./video85b.txt 66 total You've been busy. See video91.txt for a list of files Here are your last five files. Nice work! video73a.txt video75.txt video76.txt video85a.txt video85b.txt $ _

And there's our output. As you can see, the explanation, it wrote to the file and gave us the last 5 lines. Very good.

A Final Tip

Okay now you know how to use tee. And you know the syntax for commands, options and arguments.

One last tip about the tee command. So the append option -a is especially helpful if you're interested in specific entries to logs, for example.

Okay, thanks for visiting today. I hope this was a helpful introduction to the tee command.

Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:

What's Next?

Our YouTube Channel has a growing group of very smart people sharing knowledge. We'd love for you to be a part of it. Subscribe now before you get distracted.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / tee-command.html

linux tee command
linux tee examples
tee linux
tee command
tee append
sudo tee
shell tee
tee example
tee command linux
tee stdout
tee stderr
man tee
tee in shell script
tee command linux
bash tee
shell script
bash stript
redirection linux
tee bash
redirect to a file
redirect stdout
redirect stderr
unix tee
tee unix

A newly-updated free resource. Connect and refer a friend today.