The T-shaped pipe
The command tee reads standard input and writes every byte to standard output and to each file named on its command line. Its name comes from the plumbing fitting that splits one flow into two. The copy it writes to a file is identical to the copy it passes on, so the pipeline continues as if tee were not there.
tee is useful for three jobs: saving an intermediate result, showing progress on the terminal while also keeping a record, and feeding one stream to two different consumers. The third job is the most powerful and is covered at the end of this lesson.
printf 'one\ntwo\n' | tee copy.txt | wc -lThe output is 2, which is the count from wc, and the file copy.txt now holds both lines. The data passed through tee and reached wc unchanged, while a copy went to the file.
Appending with -a
By default tee truncates the file, just as the > redirection does. The -a option appends instead, which makes it suitable for building a log across several runs. Without the option, every run replaces the previous log.
echo run1 | tee -a run.log > /dev/null
echo run2 | tee -a run.log > /dev/null
cat run.logThe file holds run1 and then run2. The output of tee is sent to /dev/null here, so only the file shows the result, which is a common pattern for silent logging.
Saving a stage of a long pipeline
Suppose a pipeline filters a log and you want to keep the filtered lines before counting them. Insert tee at the point where the lines are final, and the saved file contains exactly what the next stage received.
printf 'INFO a\nERROR b\nERROR c\n' | grep ERROR | tee errors.txt | wc -lThe output is 2, and errors.txt contains the two ERROR lines. If you later need to debug the count, the file shows the exact input that wc received, which is easier than rerunning the whole chain.
Writing to the terminal and a file
Watching a long command while also saving its output is a natural use of tee. The output reaches the screen as it is produced, so you see progress, and the file keeps the complete record. The same is true when a command writes to standard output and you want standard error in the log as well.
ls /workspace /no/such 2>&1 | tee session.logBoth the listing and the error appear on the terminal, and the same two streams are saved in session.log. The merge with 2>&1 is needed because tee only sees standard output.
Feeding two consumers
tee can send a stream to a file and also to another command. Writing a process substitution after the file name sends the copy into a second pipeline. The process substitution is covered in full in its own lesson, so the example here is only a preview.
printf 'a\nb\n' | tee copy.txt >(wc -l) > /dev/nullThe count from the second consumer is printed, and copy.txt holds both lines. The output to standard output was discarded, so only the count appears. This pattern gives you one producer and several independent consumers.
tee writes to standard output and to each file in order. If one of the files cannot be opened, tee reports the error, keeps passing the data to the others and to standard output, and exits with a non-zero status.
Keeping the status of a stage
Because tee exits with its own status, a pipeline that ends in tee reports tee's result, which is nearly always success. When the stage before tee fails, that failure is hidden unless you check it. The fix is to enable pipefail or to read PIPESTATUS right after the pipeline, before any other command runs and overwrites it.
ls /no/such 2>&1 | tee errors.log > /dev/null; echo "ls=${PIPESTATUS[0]} tee=${PIPESTATUS[1]}"The output shows ls=2 and tee=0, which tells you the listing failed while the logging worked. Reading the statuses separately is the reliable way to handle a log that must be kept even when a command fails.
Splitting by size with split
A related idea is to split a large stream into several files. The split utility writes fixed-size pieces, which is useful for preparing data for a transfer that has size limits. Its output names are predictable, so the pieces can be joined back with cat.
printf '1\n2\n3\n4\n5\n' | split -l 2 - part-
ls part-*
cat part-* | wc -lThe files are named part-aa, part-ab, and part-ac, each holding up to two lines. Concatenating them restores all five lines, which the count confirms.