100 Useful Command-Line Utilities

by Oliver; 2014

76. tee

As discussed in An Introduction to the Command-Line (on Unix-like systems) - Piping in Unix, tee is a command sometimes seen in unix pipelines. Suppose we have a file test.txt such that:
$ cat test.txt
1	c
3	c
2	t
1	c
Then:
$ cat test.txt | sort -u | tee tmp.txt | wc -l
3
$ cat tmp.txt
1	c
2	t
3	c
tee, in rough analogy with a plumber's tee fitting, allows us to save a file in the middle of the pipeline and keep going. In this case, the output of sort is both saved as tmp.txt and passed through the pipe to wc -l, which counts the lines of the file.

Another similar example:
$ echo joe | tee test.txt
joe
$ cat test.txt 
joe
The same idea: joe is echoed to std:out as well as saved in the file test.txt.

<PREV   NEXT>