$_ CLI In Depth
Learning progress 0%
lesson streams redirection pipes

tee & Splitting Output

4 min read Module 7: Streams, Redirection & Pipes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:08

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.

BASH1 lines
printf 'one\ntwo\n' | tee copy.txt | wc -l

The 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.

BASH3 lines
echo run1 | tee -a run.log > /dev/null
echo run2 | tee -a run.log > /dev/null
cat run.log

The 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.

BASH1 lines
printf 'INFO a\nERROR b\nERROR c\n' | grep ERROR | tee errors.txt | wc -l

The 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.

BASH1 lines
ls /workspace /no/such 2>&1 | tee session.log

Both 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.

BASH1 lines
printf 'a\nb\n' | tee copy.txt >(wc -l) > /dev/null

The 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.

Note

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.

BASH3 lines
printf '1\n2\n3\n4\n5\n' | split -l 2 - part-
ls part-*
cat part-* | wc -l

The 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.

How a pipeline is wired

Visualizer

A pipe is a kernel buffer with a write end and a read end. The shell creates it, forks one child per command, and wires each child to its end before exec.

1 / 6
yeswrites "y" forever
head -1reads one line, exits

Two commands joined by |. Both are started, and the pipe connects their standard streams. Nothing runs one after the other.

yes | head -1; echo "${PIPESTATUS[*]}"
Terminal lab · 2 steps · real Linux in your browser

Save a checkpoint

Use tee to save a stage and to append to a log.

01
Save a stage in the middle
Copy the two lines to copy.txt on the way to wc, and save the count: ``bash printf 'a\nb\n' | tee copy.txt | wc -l > count.txt ``
Show hint for step 1
tee writes to the file and passes the same bytes on to wc.
02
Append runs to one log
Add two runs to run.log, keeping the first run when the second is written: ``bash echo run1 | tee -a run.log > /dev/null echo run2 | tee -a run.log > /dev/null ``
Show hint for step 2
Without -a the second tee would truncate the file.
Check yourself Question 1 of 4
Concept Check The -a option recall

What does the -a option do for tee?

Recap & Key Takeaways

tee copies standard input to standard output and to each named file, so a pipeline continues unchanged while a copy is saved.
Use tee -a to append to a log, and remember that without -a the file is truncated.
Merge standard error with 2>&1 before tee when the saved record must include diagnostics.
tee can feed one stream to a file and to another command, which lets one producer serve several consumers.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue