TEE Command in Linux

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

TEE Command in Linux
As per the image shown, 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'
# cat file-1.txt | tee file-2.txt

This will overwrite all the contents of the file-2.txt with file-1.txt.
if want to keep the contents of the file-2.txt intact so you need -a option to append. As shown below.

# cat file-1.txt | tee -a file-2.txt

Redirecting out of a command to a file. Most of us have a habit of using >> to redirect the output of a command to a file. But that works all the time when there is an error in the script.

So, to redirect the output of a command to a file you need to use the below command.
For example, 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.

tee command is not limited to 1 file, you should be able to write it to multiple files, just add the files using space.

[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

Leave a Comment