tee command in Linux with examples
Last Updated :
12 Jul, 2024
tee command
reads the standard input and writes it to both the standard output and one or more files. The command is named after the T-splitter used in plumbing. It basically breaks the output of a program so that it can be both displayed and saved in a file. It does both the tasks simultaneously, copies the result into the specified files or variables and also display the result.

SYNTAX:
tee [OPTION]... [FILE]...
Options :
1.-a Option :
It basically do not overwrite the file but append to the given file. Suppose we have
file1.txt
Input: geek
for
geeks
and
file2.txt
Input:geeks
for
geeks
SYNTAX :
geek@HP:~$ wc -l file1.txt|tee -a file2.txt
OUTPUT :
3 file1.txt
geek@HP:~$cat file2.txt
OUTPUT:
geeks
for
geeks
3 file1.txt

2.–help Option :
It gives the help message and exit.
SYNTAX :
geek@HP:~$ tee --help

3.–version Option :
It gives the version information and exit.
SYNTAX :
geek@HP:~$ tee --version

Application
Suppose we want to count number of characters in our file and also want to save the output to new text file so to do both activities at same time, we use tee command.
geek@HP:~$ wc -l file1.txt| tee file2.txt
OUTPUT:
geek@HP:~$15 file1.txt
Here we have file1 with 15 characters, so the output will be 15 and the output will be stored to file2. In order to check the output we use :
geek@HP:~$ cat file2.txt
OUTPUT:
geek@HP:~$15 file1.txt

?t=131
tee command in Linux with examples – FAQs
What is the tee command in Linux?
The tee command in Linux reads from standard input and writes to standard output and one or more files simultaneously. It is useful for capturing output while still displaying it in the terminal.
What is the use of tee command in Linux?
The tee command is used to split the output of a program so that it can be both displayed on the screen and saved to a file. For example:
ls -l | tee output.txt
This command will display the output of ls -l and save it to output.txt at the same time.
Does tee create a new file?
Yes, the tee command creates a new file if it does not exist. If the file already exists, it will overwrite the file unless the -a (append) option is used.
Does tee overwrite a file?
By default, the tee command overwrites the file. To append to the file instead of overwriting it, use the -a option:
ls -l | tee -a output.txt
Please Login to comment...