8 Linux Tee Command with Examples

linux tee Command

Command chaining and redirection are the features that are used for these complex tasks, You can redirect the output and/or errors to a file and it will not be shown on the terminal. Similarly, using chaining, the output of one command can be given as input to the second command and further to the third command and so on, But once you redirect an output to a file, you cannot chain it with other commands.

Linux Tee command can be used to achieve both of these results together, i.e. store the result to a file while chaining the same output to another command.

In this article, we will discuss the basics of the tee command, it's worth mentioning that all examples used in this article have been tested on Ubuntu 18.04 LTS.

Syntax Of Tee command

The tee command basically reads from the standard input and writes to standard output and files. Following is the syntax of the command:

$ command | tee [OPTION]... [FILE]...

The following examples should give us a better idea on how the command works.

1) How to write to standard output and a file

To write to standard output and a file use tee after a pipe and specify the file.

$ sudo ls /home/smart | tee folders_of_smart.txt

Now we can go to folders_of_smart.txt and see contents of the file by typing the command below.

$ sudo nano folders_of_smart.txt
  Output:
  folders_of_smart.txt
  foo.txt
  output.txt
  rootlisting.txt

2) How to write to a file and append output

To append output to exsiting file, We will use -a option.

$ sudo echo 'Movies' | tee -a folders_of_smart.txt

Now we can go to folders_of_smart.txt and see contents of the file by typing the command below.

$ sudo nano folders_of_smart.txt
  Output:
  folders_of_smart.txt
  foo.txt
  output.txt
  rootlisting.txt
  Movies

3) How to write to multiple files

To write to multiple files, We just have to mention their names in the command line.

$ sudo ping facebook.com | tee output1.txt output2.txt output3.txt

Now we can go to output1.txt, output2.txt and see contents of the file by typing the command below.

$ sudo nano output1.txt
$ sudo nano output2.txt
  Output:
  PING facebook.com(edge-star-mini6-shv-02-dfw5.facebook.com (2a03:2880:f134:183:face:b00c:0:25de)) 5$
  64 bytes from edge-star-mini6-shv-02-dfw5.facebook.com (2a03:2880:f134:183:face:b00c:0:25de): icmp_$
  64 bytes from edge-star-mini6-shv-02-dfw5.facebook.com (2a03:2880:f134:183:face:b00c:0:25de): icmp_$

4) How to redirect output of one command to another

We can also pass on the output as input to other commands. For example, the following command will not list all of the folder names in folders_of_smart.txt but list through grep ^o command the folder start with letter o.

$ sudo ls /home/smart | tee folders_of_smart.txt | grep ^o
  output:
  output1.txt
  output2.txt
  output3.txt
  output.txt

Another example, we will list /etc directories and then the output is stored in the file named stage1.txt, This output is filtered through grep command for the lines starting with letter i, This filtered output is stored in the file stage2.txt.

Finally, the filtered output is reverse sorted using sort -r command. This final output is displayed on the terminal.

$ ls /etc | tee stage1.txt | grep ^i | tee stage2.txt | sort -r
  output:
  issue.net
  issue
  iscsi
  iproute2
  inputrc
  initramfs-tools
  init.d
  init

5) How to watch log files and write to a file

We can analyse the HTTP2 traffic going through an apache2 server by Using tee command, The live logs can be streamed to standard output and also write to a file for further analysis later.

$ sudo tail -f /var/log/apache2/access.log | grep --line-buffered "HTTP/2.0" | tee -a http2.log

6) How to write to a privileged file using tee

When we want writting to file owned by root like folder_of_root.txt, We will use sudo before tee command like the example below.

$ sudo echo 'Documents' | sudo tee -a folder_of_root.txt

We will be asked for root password, Enter the root password and the changes will be saved in folder_of_root.txt.

7) How To Use tee Command With Python Script

We can use tee command with python script to store the output in file and display the output on the screen, First we will write python script to calculate area of the circle as below and save it in area.py file.

    # -*- coding: utf-8 -*-
    """This script calculate the area of circle"""
    from math import pi as pi
    from math import ceil as ceil

    radius = 2.5

    area = pi * radius**2

    print('The area of circle is', ceil(area))

We will change the value of radius and append the output to area.txt.

$ python area.py | tee -a area.txt
    Output
    ('The area of circle is for radius 2', 13.0)
    ('The area of circle is for radius 2.5', 20.0)
    ('The area of circle is for radius 3', 29.0)
    ('The area of circle is for radius 3.5', 39.0)

8) How To Use tee Command With Bash Script

We can use tee command with bash script to store the output in file and display the output on the screen, First we will write bash script to display the output of this commands pwd, ls and $HOME as below and save it in bash.txt file.

    #!/bin/bash
    echo "`pwd`";
    echo "`ls`";
    echo "$HOME";
$ ./bash.txt | tee smart.txt
    Output
    /media/disk
    bash.txt
    flask.pdf
    smart.py
    smart.txt
    VirtualBox-5.2-5.2.14_123301_el6-1.x86_64.rpm
    /home/smart

Read Also:

The tee command is very astonishing tool, we can read the standard input and write it to both the standard output and one or more files.

Ahmed Abdalhamid 12:05 am

About Ahmed Abdalhamid

Ahmed is communication engineer since 2001, he had a lot of skills. He has worked Linux system administrator, Python developer for 7 years and now he is Data scientist since 2015. He thinks Linux and Python are the greatest inventions of mankind.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

2 Comments

  1. One of the most useful things you can do with `tee` if you're scripting: write to stderr and your error log file simultaneously:

    `tee -a /proc/self/fd/2 /path/to/error.log >/dev/null`