FIELD NOTES / UNIX & LINUXNew tools on weekdays. Beyond the man page.
$tee

tee: Keep the whole stream while inspecting one part

Save a complete pipeline stream while filtering a useful view, and learn to check the errors and status that the copy alone cannot reveal.

THE DEEP DIVE12 MIN READ
01

First, make one copy

When a diagnostic command produces more than you want on screen, filtering it is tempting. But a filter throws away context. GNU tee lets you save the complete stream at the point where you branch it, while the pipeline continues to a reader. That solves the copying problem; it leaves separate questions about failed commands, failed destinations, and whether the source was trustworthy.

The examples use GNU Coreutils 9.4 tee on Ubuntu and Bash 5.2.21. Work in a disposable directory. mktemp -d creates one and prints its path; cd changes your shell's current directory to it. If you want to leave afterwards, run cd - to return to the previous directory. The synthetic events below contain no actual account data.

tee reads standard input and writes the same bytes to standard output and each named file. Standard output normally goes to your terminal; in a pipeline it goes to the next command. An existing output file is overwritten by default. Start with four fixed log records:

TERMINALCommand
cd "$(mktemp -d)"
printf '%s\n' \
  '2026-10-20T09:00:00Z INFO start' \
  '2026-10-20T09:00:01Z DENIED user=guest path=/admin' \
  '2026-10-20T09:00:02Z WARN retry' \
  '2026-10-20T09:00:03Z INFO ready' |
  tee capture.log
TERMINALOutput
2026-10-20T09:00:00Z INFO start
2026-10-20T09:00:01Z DENIED user=guest path=/admin
2026-10-20T09:00:02Z WARN retry
2026-10-20T09:00:03Z INFO ready

The shell's | sends printf's output to tee's input. Each backslash at the end of a line tells the shell to continue the same command on the next line; it is not part of the log data. printf uses the fixed format %s\n to print each quoted argument followed by a newline; the printf article explains why data belongs in arguments instead of the format. tee capture.log creates a file containing those same four newline-terminated lines and also prints them. The filename is an output operand, not an input file. The quoted $(mktemp -d) is Bash command substitution: it uses the path printed by mktemp as one argument to cd, even if the path contains spaces. Do not use this temporary directory for evidence you need to retain.

02

Show one line but retain four

Suppose the immediate question is which request was denied. Keep the full four-line baseline in raw.log while a filter narrows the terminal view:

TERMINALCommand
cat capture.log | tee raw.log | grep -F DENIED
TERMINALOutput
2026-10-20T09:00:01Z DENIED user=guest path=/admin

cat reads and prints capture.log; the first pipe carries all four lines into tee. tee copies the stream to raw.log and forwards it through the second pipe to grep; it does not finish one output before beginning the other. grep -F treats DENIED as fixed text rather than a regular expression, then prints only the matching line. Our grep deep dive covers that distinction; no regex knowledge is needed here. grep changes the view after the branch, so raw.log still has the complete supplied stream. This is useful when examining incident logs supplied for an authorized assessment: you can browse a focused signal while retaining the supplied context. The copy is only as complete as the bytes that reached tee.

03

Send the stream to more than one file

tee accepts multiple output filenames, and stdout remains another output. This command overwrites or creates two files with the four-line baseline while discarding the terminal copy:

TERMINALCommand
tee review.log handoff.log < raw.log > /dev/null

There is no terminal output. Both files contain the four lines from raw.log. The shell's < raw.log supplies stdin to tee; > /dev/null directs stdout to a discard device, rather than changing what the two named files receive. The order of destination names does not designate a primary copy. Two files in the same temporary directory are convenient working copies, not independent proof of origin or protection from later edits.

04

Append a second batch, deliberately

The default overwrite behavior matters on the second run. Here we add one new, clearly synthetic event to capture.log without removing its original four lines:

TERMINALCommand
printf '%s\n' '2026-10-20T09:05:00Z WARN second-pass' | tee -a capture.log
TERMINALOutput
2026-10-20T09:05:00Z WARN second-pass

-a means append to each named file instead of truncating it. capture.log now has five lines, and the displayed line is the new one. raw.log, review.log, and handoff.log still have the original four. Without -a, this command would replace capture.log with only the second-pass line. Appending does not identify where one run ended and another began unless the upstream data carries that context; the timestamp here is sample input, not something tee supplied.

05

Remember the separate error stream

Pipes carry stdout by default. A command's stderr, file descriptor 2, ordinarily goes directly to the terminal. We can route that error stream to a separate file and inspect it without relying on a real command failure:

TERMINALCommand
bash -c 'printf "%s\n" "record"; printf "%s\n" "diagnostic" >&2' 2> producer.err | tee stdout.log
cat producer.err
TERMINALOutput
record
diagnostic

The first printf writes record to stdout. >&2 sends the second printf to descriptor 2. The outer shell's 2> producer.err captures that stream before the pipe; tee stdout.log receives only record. The later cat prints the saved diagnostic, accounting for the second displayed line. The temporary file producer.err contains diagnostic, while stdout.log contains record. The nested bash -c runs the quoted commands in a separate Bash process; this makes both streams explicit for the exercise. If you intentionally want both streams in one capture, place 2>&1 on the producing command before the pipe. That Bash redirection points descriptor 2 at the producer's current stdout, which is the pipe. It changes what you collect; when a real program buffers stdout and stderr differently, their relative order can still surprise you. Keep the streams separate when that distinction matters.

06

A saved file does not mean the producer succeeded

The shell ordinarily reports a pipeline's last command status. tee can succeed after an upstream command fails, including when it receives no bytes:

TERMINALCommand
(
  set +o pipefail
  false | tee failed-default.log > /dev/null
  printf 'default pipeline status=%s\n' "$?"
  set -o pipefail
  false | tee failed-pipefail.log > /dev/null
  printf 'pipefail status=%s\n' "$?"
)
TERMINALOutput
default pipeline status=0
pipefail status=1

false is a shell command whose only purpose here is to fail with status 1. Both tee commands successfully create empty files. $? is the status of the immediately preceding pipeline, so each printf must follow its pipeline directly. In Bash, set +o pipefail turns off the optional pipeline failure rule, giving the status of the rightmost command (tee, zero here). set -o pipefail turns it on: Bash then returns the rightmost nonzero status in the pipeline (the producer's 1 here). Parentheses run these shell settings in a subshell, leaving your interactive shell unchanged. pipefail is a Bash feature, not a tee option or a claim about the quality of the captured data.

07

Check whether a destination failed

Even when stdout and one file succeed, another destination can fail. On Linux, /dev/full accepts an open but rejects writes with a no-space error. This disposable test uses it as a deliberately failing sink and asks GNU tee to diagnose output errors:

TERMINALCommand
tee --output-error=warn saved.log /dev/full < raw.log > /dev/null
printf 'tee status=%s\n' "$?"
TERMINALDiagnostic and status
tee: /dev/full: No space left on device
tee status=1

--output-error=warn tells GNU tee to warn on a failed output, including a pipe, continue writing to remaining outputs, and exit nonzero if any output failed. saved.log still contains the four original lines. The shell redirects stdin from raw.log and discards tee's stdout; the diagnostic is stderr and therefore remains visible. The printf then reports tee's status immediately. /dev/full is a Linux test device, not a real filesystem-full simulation with all its timing and recovery details. GNU tee's default also diagnoses non-pipe output errors, but its behavior when a pipe closes early differs, which becomes relevant next.

08

Keep reading when a downstream viewer exits

head -n 1 shows one line and then stops reading. In a large pipeline, that closes the stdout pipe while tee may still need to write its file. GNU tee -p selects its warn-nopipe error mode: it ignores a broken pipe as long as another output remains available, and continues reading and writing that output. Use a bounded number source so this cannot run forever:

TERMINALCommand
seq 1 20000 | tee -p numbers.log | head -n 1
wc -l < numbers.log
TERMINALOutput
1
20000

seq 1 20000 prints the integers 1 through 20000, one per line. head -n 1 shows only the first line; -n takes a line count. tee -p keeps writing numbers.log after the viewer exits. wc -l counts newline bytes, so its 20000 result checks the completed file contains that many terminated lines; < numbers.log supplies the file without displaying its name. Without -p, GNU tee exits immediately on a broken stdout pipe. Whether a small test actually triggers that error depends on pipe buffering, so the lesson is the documented error policy and the verified full-file count here, not a promised truncation size for a default-mode comparison.

09

What this copy can and cannot establish

For an authorized review of supplied output, placing tee before a filter keeps context that the filter would discard. It can help compare a selected alert with the surrounding records, or give another analyst the full supplied stream while you inspect a narrow view. It does not prove that the producer emitted every possible record, that a log source was authentic, or that nobody can modify the saved file afterwards. Capture from a trusted source, preserve its provenance, and apply separate integrity and access controls where evidence requires them.

tee writes with the permissions of the process running it. An administrator may intentionally run tee with elevated privileges to write a protected file after an unprivileged producer writes to the pipe; that is a real capability and a reason to review the destination carefully. It is not by itself a vulnerability in tee or evidence of an exploit. The shell's > redirection happens under the shell's own privileges, a distinction that explains why elevating only tee differs from elevating the shell. Do not point this article's disposable examples at system configuration files.

10

Use the branch for a focused review

The working pattern is short once the boundary is clear. With raw.log still holding the original four lines, this command creates a fresh full copy while showing the denied request:

TERMINALCommand
cat raw.log | tee final-capture.log | grep -F DENIED
TERMINALOutput
2026-10-20T09:00:01Z DENIED user=guest path=/admin

final-capture.log has all four lines. The terminal has one. This command overwrites an existing final-capture.log; use append only when combining runs is intentional. For scripted work, check the relevant command and destination statuses instead of inferring success from a visible line. tee gives you a complete copy of what entered its stdin, a point where you can put a file beside a continuing pipeline; trust and success require their own checks.

Back to latest issue
HELP SHAPE THE NEXT ISSUE

What belongs in your toolbox?

Choose a command you want to know better. We start with familiar tools and work toward the less obvious ones.

READER’S CHOICE

What should we cover next?

Vote for a future deep dive, or suggest a command we missed.

One command name, up to 32 characters. Suggestions are reviewed by a person. A new article starts a fresh poll.

Loading poll…