
Linux tee command is used for chaining and redirection of 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 output to a file, you cannot chain it with other commands.
Linux tee 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 tutorial, we will discuss how to use Linux tee with some examples.
Tee Command Syntax
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 the existing 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 analyze 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 writing 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
The tee command is a very astonishing tool, we can read the standard input and write it to both the standard output and one or more files.
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`
Excellent. Thanks Gray for the information