TEE Command in Linux is useful when you want to redirect the standard output to a file. What it actually does is redirect output to the file and as well to the screen(STDOUT). It already comes pre-installed on Linux distributions. If it isn’t installed in the rare cases, refer to this for installation.
As you see in the image above, TEE redirects the output to the file as well as the stdout. You should be easily able to check the tee command version using tee --version
.
# tee --version
tee (GNU coreutils) 8.4
Copyright (C) 2010 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.
How to use Tee Command?
To use the TEE Command in Linux you will have to use the syntax below.
tee [OPTION]... [FILE]...
Help Options will give you more details on how to use it.
# 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.
Report tee bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report tee translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'tee invocation'
Examples:
# cat file-1.txt | tee file-2.txt
a
b
c
This will overwrite all the contents of the file-2.txt with file-1.txt. If you want to keep the contents of the file-2.txt intact, use the -a
option to append, as shown below.
# cat file-1.txt | tee -a file-2.txt
one
word
Most of us have a habit of using >>
to redirect the output of a command to a file. However, this does not work when there is an error in the script, as >>
only redirects standard output, not errors
So, to redirect the output of a command to a file, you need to use the below command. For example, if my script has an error but when I use >>
to redirect output, it won’t work.
# ./servicestatus.sh >> error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
# ls -al error.log
-rw-r--r-- 1 root root 0 May 29 06:05 error.log
My error log file is blank, so here I will use the TEE command which works perfectly. It will re-direct stdout and stderr into a file.
# ./servicestatus.sh 2>&1 | tee error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
# ls -al error.log
-rw-r--r-- 1 root root 66 May 29 06:08 error.log
# cat error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
Writing to multiple files
The tee
command is not limited to 1 file; you can write to multiple files by adding them with spaces.
[command] | tee [options] [filename1] [filename2]...
There are many other practical uses of the tee command in Linux. Imagine you want to count the number of the lines in the file and as well save the output at the same time. You would have written a script without the TEE command.
# wc -l test.txt | tee number_of_lines.txt
8 test.txt
# cat number_of_lines.txt
8 test.txt
Additional Examples of tee
Command
Suppress output with /dev/null
Sometimes, you might want to save the output to a file while suppressing it from being displayed on the screen. This can be done using /dev/null
.
# echo "This will go to file only" | tee file.txt >/dev/null
Here, tee
writes to file.txt
without displaying the output to the screen.