Introduction
The tee command reads standard input (stdin) and writes the result to standard output (stdout) and one or more files simultaneously.
It basically displays the command output on the terminal and at the same time redirects the output to a file. I often used tee command to append a new line to the root user files.
We’ll show how to use the tee command on Linux.
The basic syntax for the tee command is:
[command] | tee [options] [filename]
Usage of tee command
One of the basic uses of the tee command is to display the standard output (stdout) of a command and save it in a file. For instance, we have executed the ‘hostnamectl’ command to print our system’s hostname and other details, then save the standard output to ‘unixcop.txt’.
[root@unixcop ~]# hostnamectl | tee unixcop.txt
Static hostname: unixcop
Icon name: computer-vm
Chassis: vm
Machine ID: 7c8b674cab8340828e333aac68fa4a51
Boot ID: ed756f0df66d4ed7aef6890f68b1e672
Virtualization: kvm
Operating System: CentOS Linux 8
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-305.10.2.el8_4.x86_64
Architecture: x86-64
[root@unixcop ~]#
The content of the ‘unixcop.txt’ file can be viewed using the cat command as follows:
[root@unixcop ~]# cat unixcop.txt
Static hostname: unixcop
Icon name: computer-vm
Chassis: vm
Machine ID: 7c8b674cab8340828e333aac68fa4a51
Boot ID: ed756f0df66d4ed7aef6890f68b1e672
Virtualization: kvm
Operating System: CentOS Linux 8
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-305.10.2.el8_4.x86_64
Architecture: x86-64
[root@unixcop ~]#
Write output to multiple files
This can also write output to multiple files. To do so, add a list of files separated by space as arguments.
# command | tee file1 file2…fileN
For example:
[root@unixcop ~]# cal | tee file1 file2 file3
August 2021
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
[root@unixcop ~]# ls
anaconda-ks.cfg file1 file2 file3 unixcop.txt
[root@unixcop ~]#
[root@unixcop ~]# cat file1 file2 file3
August 2021
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
August 2021
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
August 2021
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
[root@unixcop ~]#
Append output to a file
By default, the tee command overwrites the contents of a file, each time you run it. To append output, use the ‘-a or –append’ option.
# command | tee -a file
Ignore Interrupts
This option allows tee command to exit gracefully when the command has been interrupted with (CTRL+C) during execution. To do so, use the ‘-i or –ignore-interrupts’ option
# command | tee -i file
Hide the output
If you don’t want to print the command result to a standard output, use the following format:
# command | tee file >/dev/null
Use of tee command in shell script
The tee command can often be used in shell scripts to redirect the output to a file as shown below:
# vi /tmp/testbash.sh
#!/bin/bash
LOGFILE=/tmp/test-logs-$(date +%d%m%Y)
echo "Append the logs " | tee -a $LOGFILE
Tee help Option
It gives the help message and exit.
SYNTAX :
# tee --help
[root@unixcop ~]# 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
-p diagnose errors writing to non pipes
--output-error[=MODE] set behavior on write error. See MODE below
--help display this help and exit
--version output version information and exit
MODE determines behavior with write errors on the outputs:
'warn' diagnose errors writing to any output
'warn-nopipe' diagnose errors writing to any output not a pipe
'exit' exit on error writing to any output
'exit-nopipe' exit on error writing to any output not a pipe
The default MODE for the -p option is 'warn-nopipe'.
The default operation when --output-error is not specified, is to
exit immediately on error writing to a pipe, and diagnose errors
writing to non pipe outputs.
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report tee translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/tee>
or available locally via: info '(coreutils) tee invocation'
[root@unixcop ~]#
Conclusion
In this guide, we’ve shown you how to use tee command in Linux.