Table of Contents
In this article, we will see 10 Awesome tee command examples in Linux which can be good learning for beginners. tee command is a very useful Linux based command line interpreter which takes input from standard input streams and writes it to both standard output as well to one or more files. Sometimes it can also take the output of another command and pass it as input to some other command using pipe operator. It is also very handy with bash scripts. We will understand more about this utility using some real world examples in below section.
Synopsis
tee [OPTION]... [FILE]...
10 Awesome tee command examples in Linux
Also Read: 20 Practical echo command examples in Linux for Beginners
Example 1: How to Check tee command version
If you want to check the current tee command version then you need to use tee --version
command as shown below. As you can see current tee command version is 8.22
.
[root@localhost ~]# tee --version tee (GNU coreutils) 8.22 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Parker, Richard M. Stallman, and David MacKenzie.
Example 2: How to use tee utility in Linux
You can use tee utility to save the command output to a file like we have shown in below example. Here we are saving the output ps
command to process.txt
file using ps | tee process.txt
command.
[root@localhost ~]# ps | tee process.txt PID TTY TIME CMD 2327 pts/1 00:00:00 bash 2490 pts/1 00:00:00 ps 2491 pts/1 00:00:00 tee
If you open process.txt
file then you will be able to see ps
command output saved in this file.
[root@localhost ~]# cat process.txt PID TTY TIME CMD 2327 pts/1 00:00:00 bash 2490 pts/1 00:00:00 ps 2491 pts/1 00:00:00 tee
Example 3: How to Append all the output instead of overwriting
If you want to keep saving all the contents to a file and do not want to overwrite then you can think of using -a
option with tee command as shown in below example. Here we are saving the output of ps
command to process.txt file using ps | tee -a process.txt
. More on tee command Man Page.
[root@localhost ~]# ps | tee -a process.txt PID TTY TIME CMD 2327 pts/1 00:00:00 bash 2513 pts/1 00:00:00 ps 2514 pts/1 00:00:00 tee
If you now check the output of process.txt
file, it will show the previous contents as well along with the latest data.
[root@localhost ~]# cat process.txt PID TTY TIME CMD 2327 pts/1 00:00:00 bash 2510 pts/1 00:00:00 ps 2511 pts/1 00:00:00 tee PID TTY TIME CMD 2327 pts/1 00:00:00 bash 2513 pts/1 00:00:00 ps 2514 pts/1 00:00:00 tee
Example 4: How to Save command output to Multiple Files
Using tee command, you can save the command output to not only one file but to multiple files by just adding the files name as shown in below example. Here we are saving the output of ps
command to process1.txt
and process2.txt
file using ps | tee process1.txt process2.txt
command.
[root@localhost ~]# ps | tee process1.txt process2.txt PID TTY TIME CMD 2327 pts/1 00:00:00 bash 2545 pts/1 00:00:00 ps 2546 pts/1 00:00:00 tee
If you open both process1.txt
and process2.txt
file using cat
utility, then you will see ps
command output on both the files as shown below.
[root@localhost ~]# cat process1.txt PID TTY TIME CMD 2327 pts/1 00:00:00 bash 2545 pts/1 00:00:00 ps 2546 pts/1 00:00:00 tee [root@localhost ~]# cat process2.txt PID TTY TIME CMD 2327 pts/1 00:00:00 bash 2545 pts/1 00:00:00 ps 2546 pts/1 00:00:00 tee
Example 5: How to Suppress tee command output
If you want tee command to only save the contents in a file and not show it on the output then you can redirect all the contents to /dev/null
filesystem as shown below.
[root@localhost ~]# ps | tee process.txt > /dev/null
Example 6: How to Check all the options available with tee command
If you want to check all the options available with tee command then you need to use tee --help
command as shown below.
[root@localhost ~]# tee --help Usage: tee [OPTION]... [FILE]... Copy standard input to each FILE, and also to standard output. -a, --append append to the given FILEs, do not overwrite -i, --ignore-interrupts ignore interrupt signals --help display this help and exit --version output version information and exit If a FILE is -, copy again to standard output. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'tee invocation'
Example 7: How to Pass command output as an Input to Another command
If you want to pass output of a command as an input to another command then you need to use tee command like below. Here tee
command takes the output from ps
utility and pass it as input to wc -l
command along with the task of saving the ps command output to process.txt file. All these in just one simple command.
[root@localhost ~]# ps | tee process.txt | wc -l 5
Example 8: How to use tee command in Bash Script
You can also use tee command in a bash script. We have shown here with an example where we have created a simple example.sh
script.
[root@localhost ~]# vi example.sh #!/bin/bash ps | tee process.txt
After saving the file, you need to provide execute permission to this script using chmod +x example.sh
command.
[root@localhost ~]# chmod +x example.sh
Once you run the script, it should show output like below.
[root@localhost ~]# ./example.sh PID TTY TIME CMD 1327 pts/0 00:00:00 bash 1346 pts/0 00:00:00 example.sh 1347 pts/0 00:00:00 ps 1348 pts/0 00:00:00 tee
Example 9: How to make tee command Ignore Interrupt Signal
If you want tee command to ignore any interrupt signals given from keyboard, then you need to use -i
option with tee command as shown in below example. Here we are pinging to google.com
and saving all the output to google.txt
file. In between we are giving Ctrl+C
interrupt signal which is visible on the output.
[root@localhost ~]# ping google.com | tee -i google.txt PING google.com (142.250.194.174) 56(84) bytes of data. 64 bytes from del12s06-in-f14.1e100.net (142.250.194.174): icmp_seq=1 ttl=113 time=70.4 ms 64 bytes from del12s06-in-f14.1e100.net (142.250.194.174): icmp_seq=2 ttl=113 time=70.0 ms 64 bytes from del12s06-in-f14.1e100.net (142.250.194.174): icmp_seq=3 ttl=113 time=70.2 ms 64 bytes from del12s06-in-f14.1e100.net (142.250.194.174): icmp_seq=4 ttl=113 time=70.3 ms ^C --- google.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3007ms rtt min/avg/max/mdev = 70.085/70.279/70.495/0.304 ms
Now If you open google.txt
file and check, you will not see any interrupt signal in google.txt
file. This confirms that tee command ignored the interrupt given from the Keyboard.
[root@localhost ~]# cat google.txt PING google.com (142.250.194.174) 56(84) bytes of data. 64 bytes from del12s06-in-f14.1e100.net (142.250.194.174): icmp_seq=1 ttl=113 time=70.4 ms 64 bytes from del12s06-in-f14.1e100.net (142.250.194.174): icmp_seq=2 ttl=113 time=70.0 ms 64 bytes from del12s06-in-f14.1e100.net (142.250.194.174): icmp_seq=3 ttl=113 time=70.2 ms 64 bytes from del12s06-in-f14.1e100.net (142.250.194.174): icmp_seq=4 ttl=113 time=70.3 ms --- google.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3007ms rtt min/avg/max/mdev = 70.085/70.279/70.495/0.304 ms
Example 10: How to check Man page of tee command
If you want to check the man page of tee command then you need to use man tee
command as shown below.
[root@localhost ~]# man tee TEE(1) User Commands TEE(1) NAME tee - read from standard input and write to standard output and files SYNOPSIS tee [OPTION]... [FILE]... DESCRIPTION Copy standard input to each FILE, and also to standard output. -a, --append append to the given FILEs, do not overwrite -i, --ignore-interrupts ignore interrupt signals --help display this help and exit