Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'tee' Command In Linux

2023-06-12 - By Robert Elder

     I use the 'tee' command to send the output from a program to multiple places at once:

echo "Hello World!" | tee test.log

Using 'tee' To Monitor & Log Output

     Here, I have a simple program in a file called 'my-cool-program' that prints information to standard output:

#!/bin/bash

while true; do
        MY_NUM=$(($RANDOM % 3))
        if [ $MY_NUM -eq 0 ]; then
                for i in {1..30}; do
                        echo "$(date):  THE PROGRAM IS RUNNING NORMALLY";
                done
        elif [ $MY_NUM -eq 1 ]; then
                echo "$(date):  Error code $RANDOM";
        elif [ $MY_NUM -eq 2 ]; then
                echo "$(date):  Warning code $RANDOM";
        else
                echo "other";
        fi
        sleep 1;
done

     When I run this program, I see output like this::

./my-cool-program
Thu 08 Jun 2023 10:57:25 AM EDT:  Warning code 17575
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
...

     If I want to save the output, I can pipe the output from this program to a log file like this:

./my-cool-program > activity.log

     but now I can't monitor what output the program is producing.

     If I use the 'tee' command like this:

./my-cool-program | tee activity.log
Thu 08 Jun 2023 10:57:25 AM EDT:  Warning code 17575
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
...

     all of the output from the program will be printed to the terminal, but it will also be printed to the activity log file.

Append To Log File

     By default, the log file will be overwritten, but you can append to it instead using the '-a' flag:

./my-cool-program | tee -a activity.log
Thu 08 Jun 2023 10:57:25 AM EDT:  Warning code 17575
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 10:57:26 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
...

     Now, I can log all of the original output to the activity log but filter and manipulate the output that's displayed on the terminal.

     I can use this 'grep' statement to filter some of the verbose output:

./my-cool-program | tee -a activity.log | grep -v 'RUNNING NORMALLY'
Thu 08 Jun 2023 11:08:06 AM EDT:  Error code 3836
Thu 08 Jun 2023 11:08:07 AM EDT:  Error code 31474
Thu 08 Jun 2023 11:08:08 AM EDT:  Warning code 11422
Thu 08 Jun 2023 11:08:09 AM EDT:  Warning code 28734
Thu 08 Jun 2023 11:08:19 AM EDT:  Error code 22064
Thu 08 Jun 2023 11:08:20 AM EDT:  Warning code 18124
...

Signals & Error Handling

     The 'tee' command also supports flags that influence it's behavior when handling signals.  This use of the head command illustrates one of these differences:

./my-cool-program | tee -a activity.log | head -n 3
Thu 08 Jun 2023 11:09:09 AM EDT:  Error code 590
Thu 08 Jun 2023 11:09:10 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 11:09:10 AM EDT:  THE PROGRAM IS RUNNING NORMALL

     In the above case, the 'head' command correctly prints the first three lines of the output and the command exits normally.  However in this case:

./my-cool-program | tee --output-error=warn -a activity.log | head -n 3
Thu 08 Jun 2023 11:09:19 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 11:09:19 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
Thu 08 Jun 2023 11:09:19 AM EDT:  THE PROGRAM IS RUNNING NORMALLY
tee: 'standard output': Broken pipe

     the output ends with 'tee: 'standard output': Broken pipe' hangs until you use CTRL + C.

     And that's why the 'tee' command is my favourite Linux command.

Intro To 'stty' Command In Linux
Intro To 'stty' Command In Linux
Published 2023-10-04
Terminal Block Mining Simulation Game
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List
Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use