Back to Explore
GuideVibeFix editorialUpdated Dec 1, 2026

tee from Zero: Split One Input Two Ways and Keep Both

Data in a pipe usually has one way out; tee gives it two: watch it on screen and keep a copy in a file. This guide starts from zero on tee: the overwrite default and -a, splitting to several files at once, why tee - is a file name now, what the exit code says when a write fails, and why pipefail is needed to catch a pipeline error. Every step was run for real on GNU coreutils 9.4.

Real photograph of a copper T-shaped pipe fitting, the namesake of the tee command

Data in a pipe usually has only one way out: down to the next command, or into a file with a redirect — pick one. If you want both — watch it on screen and keep a copy in a file — you either run the command twice or save the file first and open it afterwards. tee exists for that "want both" case: it copies standard input in two directions, one copy straight to standard output and one copy into a file you name. The name is the shape: a tee is the T-shaped fitting in plumbing, one inlet and water out both sides. This guide starts from zero on tee: the simplest use, the overwrite trap, splitting to several files at once, the counterintuitive file name tee -, what the exit code says when a write fails, the error a pipeline hides from you, and process substitution for processing one input several ways at once. Every step was run for real on GNU coreutils 9.4 and bash 5.2.21, and every line of output quoted here matches the test record at the end of the article word for word.

1. The simplest use: one copy to the screen, one copy into a file

tee is one sentence: command | tee file. The test ran seq 1 5 | tee copy.txt: seq produced 1, 2, 3, 4, 5, and tee printed those 5 lines on screen while writing the same 5 lines into copy.txt. The screen showed 1, 2, 3, 4, 5, and cat copy.txt showed 1, 2, 3, 4, 5 too — identical on both sides. tee does not change the content, sort it, or filter it; it is a photocopier. What comes in goes out unchanged, only now it goes out to two places.

The most common scene for this is a command that takes a while: a build, a download, a test run, with long output you want to watch live and look back through later. The old way was to redirect into a file first and cat it after the run, with nothing on screen while it ran; or to watch the screen only, and lose the line you wanted once it had scrolled past. With a tee in the middle you get both: the screen keeps scrolling, and the file keeps a complete record. The manual gives an even sharper example: downloading a large file while computing its checksum, without reading it twice — the download stream passes through tee, one copy goes into the file and one copy goes straight to the checksum command, so both jobs finish in a single pass.

A tee in the middle of a pipeline does not block the road downstream. The test ran seq 1 5 | tee copy2.txt | sort -r: while tee wrote 1 through 5 into copy2.txt, it passed them on unchanged to sort -r, and the screen showed 5, 4, 3, 2, 1, sorted in reverse as expected. tee is transparent to whatever comes after it: whatever the upstream gives, it passes on, not one line missing and not one line changed. To take a snapshot in the middle of a pipeline without breaking the processing after it, put a tee in the middle; nothing before or after it needs to change.

2. The default is overwrite: without -a, the old content is gone

This is the first real trap in this guide: tee overwrites files by default. The test first wrote old-content-line into keep.txt, then ran seq 1 3 | tee keep.txt. The screen showed 1, 2, 3, and cat keep.txt then showed only 1, 2, 3 — old-content-line was gone. tee opens the file in overwrite mode: whatever was in the file is cleared first, and only then is the new content written. The manual says it plainly: if a file being written to already exists, the data it previously contained is overwritten unless the -a option is used.

To keep writing after what is already there, use -a. The test then ran seq 4 5 | tee -a keep.txt: the screen showed 4 and 5, and cat keep.txt showed all five lines, 1, 2, 3, 4, 5: the first 3 lines untouched, the 2 new lines joined at the end. -a is append. Remember the contrast: tee file means "clear it and write", tee -a file means "write after what is there". Logs, records, and results accumulated over several runs always need -a. If you only want the latest run, leaving -a off is fine, but first think clearly: do you still need what is in that file? When you are not sure, cat the file and look at what is inside before deciding whether to add -a.

One more detail: -a only matters when the file already exists. When the file does not exist yet, tee and tee -a do the same thing: create the file first, then write the content into it. The manual says: if a file being written to does not already exist, it is created. So the first time you write a log file, -a or no -a gives the same result; the second and third time you write into the same file, the difference appears: without -a, everything before is gone; with -a, everything before stays. In a script, the line that writes the log file should carry -a by default — leave it out, and the records from the earlier runs are wiped by the last one.

The overwrite trap has a variant: when tee fails to open the file, the old content is still there, but none of the new content gets written anywhere. Section 5 tests this: writing into a directory that does not exist makes tee fail and exit, the screen output still goes through, but nothing is left in the file. The difference between overwriting and appending only holds when the file can be opened; when it cannot be opened, there is no overwriting at all.

3. Split to several files at once; with no file it is a pass-through

tee takes more than one file, so one input can be written to several places at once. The test ran seq 1 3 | tee a.txt b.txt: the screen showed 1, 2, 3, and a.txt and b.txt each got a copy. wc -l a.txt b.txt printed 3 a.txt, 3 b.txt, and 6 total: 3 lines in each file, 6 lines altogether. One run, three results: one on screen and one in each file. To keep one copy to look at, one to archive, and one to hand to someone else, there is no need to run three times — one command is enough.

tee also runs with no file at all: seq 1 3 | tee printed 1, 2, 3 on screen with exit code 0. It is a pure pass-through: input comes in, output goes out, nothing is kept. That looks useless, but it is a good friend when debugging: when something goes wrong in the middle of a pipeline and you suspect one stretch of output, put a file-less tee in the middle to confirm the data is still right at that point, changing nothing. Or the other way round: to save a copy of the data in the pipe for a moment while you investigate, put a tee with a file in the middle, delete the file when you are done, and the pipeline itself never changes.

4. tee - is not "print it again": it is a file name now

This is the most counterintuitive trap in this guide. Give tee a dash, -, as its argument, and you might expect it to mean "send another copy to standard output" — and in old versions that is what it meant: in coreutils 5.3.0 through 8.23, a - made tee send another copy of the input to standard output. The current version changed that: the manual states it clearly, and since then tee follows POSIX and treats - as a file name. The test ran seq 1 2 | tee -: the screen showed 1 and 2, and then ls -1 showed that a file called - had appeared in the working directory, containing 1 and 2. cat ./- printed 1 and 2, proving those 2 lines really were written into the file called -.

The trouble is not tee itself; it is the habit of "a dash means a file name". In many commands, - means standard input or standard output — tar and cat, for example, read - as "read from the pipe" or "write to the screen". tee does not do that any more: write tee - and it dutifully creates a file called -. Worse, that file is awkward to delete: rm - treats - as an option and refuses, so you have to write rm ./- instead. In the test, ls -1 printed -, a.txt, b.txt, copy.txt, copy2.txt, keep.txt, and that lone - is the file this run left behind. When you see tee - from now on, first remember: this is not "print it again", it is creating a file called - in the current directory.

tee works like a T-fitting in plumbing: one input comes in, one copy goes to the screen and one copy goes into a file

5. When a write fails: the screen still prints, the exit code tells you

What tee does when a write to a file fails is interesting: the copy on screen goes out as usual, the copy to the file fails, and the exit code becomes non-zero. The test used /dev/full, a special device that answers every write with "no space left", no matter how little you write. echo hello | tee /dev/full produced two lines: the screen printed hello first, then an error line, tee: /dev/full: No space left on device, and the exit code was 1. hello still appeared, so tee did not cut off standard output just because the file write failed; the exit code of 1 shows it did record the failure.

A file that cannot be opened behaves the same way. The test ran echo hi | tee /no-such-dir/x.txt, where the directory does not exist at all: the output was an error line first, tee: /no-such-dir/x.txt: No such file or directory, then hi on screen as usual, with exit code 1. The error comes first and hi second, and that order is worth noticing: tee opens the file first, reports the failure first, and then carries on sending the input to standard output. Nothing was left in the file, and not one line was missing on screen. In a script, remember this behaviour: an exit code of 1 from tee does not mean the screen output was wrong, only that at least one output failed to be written. To find out which one, read the error line; and to see only the content without the errors, remember the errors go to standard error while normal output goes to standard output — the two were separate all along.

There is a subtler failure: when the reader at the other end quits early. If what follows tee is not a file but a process substitution or a pipe, and the downstream reader only takes the first few lines and leaves, tee's next write has nowhere to go. The manual added the -p option for exactly this: by default, an error writing to a pipe makes tee exit at once; with -p, it warns once, keeps writing to the outputs that are still open, and only stops when every remaining output has broken. You will rarely need that option, but know the switch exists: tee treats errors on pipes and errors on files differently by design.

6. The easiest error to miss in a pipeline: pipefail

This is the trap most worth remembering in this guide, and the fault is not tee's but the pipeline's. A pipeline's exit code only reflects its last command by default. The test ran false | tee pf1.txt: false is a command built to fail, with exit code always 1; tee successfully passed false's output along — which was nothing at all — and exited 0. The whole pipeline exited 0, because its last command, tee, was 0. false had clearly failed, yet the pipeline reported success. The failure was hidden.

The fix is to turn on pipefail. The test ran set -o pipefail first, then false | tee pf2.txt, and the exit code became 1. pipefail means: the pipeline's exit code is the exit code of the last command that exited non-zero, and 0 only when all of them succeeded. The manual puts it this way: if pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully. Once it is on, false's failure can no longer hide, and the whole pipeline becomes 1 with it.

This trap is at its worst in scripts. In a deploy script or a backup script, any step failing in a pipeline should stop the script, not let it carry on under an exit code of 0 and treat a bad result as a good one. The right habit is to write set -o pipefail at the top of the script, or at least turn it on before a critical pipeline and off again afterwards. The test script did exactly that: after showing what pipefail does when it is on, it ran set +o pipefail at once to restore the default. Remember the rule: whenever a failure in any command of a pipeline is one you cannot accept, turn pipefail on. Otherwise the last command's success covers it up, and you will not even know it happened.

7. One input, several kinds of processing: process substitution

tee's strongest trick is splitting the input to commands, not just to files. The form is tee >(command): a greater-than sign and parentheses wrapped around a command, and tee sends one copy of the input into that command's standard input. The test ran seq 1 5 | tee >(wc -l > count.txt) | tail -n 2: seq produced 1 through 5, tee sent one copy to wc -l to count the lines and passed the other copy on to tail -n 2. The screen showed 4 and 5, and count.txt held 5: five lines in total, counted as five, both sides right. The same input was used twice — tail took its last two lines and wc counted the total — with seq running only once.

The manual's example is more practical: downloading a large file while computing its checksum. The old way downloads the file and then reads it a second time to compute the checksum, reading a large file twice; with tee and process substitution, the download stream passes through tee, one copy goes into the file and one copy goes straight to the checksum command, downloading and checksumming at the same time, in a single pass. The test ran a small version: printf "hello tee\n" | tee >(sha256sum > sum.txt) > saved.txt. saved.txt held hello tee, and sum.txt held 008ed6740612cc95edaebff6b5512dd21d7cc8bdedbc66e3777a80cd9f3721ed -: the first part is the SHA-256 checksum of the line hello tee, and the dash after it is sha256sum's convention for "the data came from standard input, not from a file".

Process substitution has two things to watch. First, the command inside >(...) runs asynchronously: tee sends the data on and carries on, and the command inside may not have finished writing yet. That is why the test script sleeps for 1 second before cat count.txt: give wc a second to write the result into the file before reading it. In a real script that wait cannot be skipped — read too early and the file may be empty or half written. Second, the >(...) form is not in every shell: the manual says it works in bash, zsh, and ksh, but not in /bin/sh. The first line of the script should be #!/bin/bash, not #!/bin/sh, or the syntax fails outright.

The manual has one more one-pass example: compressing while you work. The du output for a large file tree is huge, and you want to look at it in a graphical tool right away and keep a compressed copy for the archive. The old way compresses all of du's output first, then decompresses it to look at it, in two separate steps; with tee and process substitution, du's output passes through tee, one copy goes straight to the graphical tool and the other is compressed by gzip into a file at the same time, in one pass. The bigger the data, the more time that single pass saves: read once and process twice is far faster than reading twice and processing twice.

8. Why sudo tee works: who opens the file in a redirect

The most famous use of tee is actually sudo tee. The scene: you need to write one line into a file only root can write, such as a system config file. The instinctive form is sudo echo "one line of config" > /etc/some-file, and it fails with permission denied. Why? Because the redirect, that greater-than sign, is opened by the calling shell before sudo even starts: the shell, still running as you, tries to open the file, cannot open root's file, and fails before sudo gets a turn. Change it to echo "one line of config" | sudo tee /etc/some-file and it works: the pipe hands the content to a tee started by sudo, tee runs as root, the file is opened by tee, and it opens fine. The screen also prints the line you just wrote, which conveniently confirms what went in.

This part needs an honest boundary: the sudo tee scenario was not tested in this guide. The machine used for the drafts runs as root, so the permission failure it solves cannot be reproduced there — root can open any file, so the "cannot open" half of the comparison never happens. What is written above is the mechanism: a redirect opens the file in the calling shell, and tee opens the file as whoever runs on the other end of the pipe. That mechanism follows from the manual and the basic rules of pipes; it is not something recorded in the test run. On a machine where you are an ordinary user, run the two commands above one after the other and the contrast appears at once: the first fails with permission denied, the second writes the file and prints the line back on screen.

9. Checklist: splitting a stream next time, follow this

First, when you want to watch it on screen and keep it in a file, use tee: command | tee file, one copy to the screen and one into the file. Second, the default is overwrite and old content is cleared; to append, use tee -a. Third, one run can split to several files with tee a.txt b.txt; with no file it is a pass-through, the content flows through unchanged and nothing is kept. Fourth, tee - is a file name now: it creates a file called - in the current directory, and removing it takes rm ./-. Fifth, when a write fails, the screen still prints and the exit code turns non-zero: /dev/full means no space, a missing directory means it cannot be opened — the error line says which output failed. Sixth, do not let a pipeline hide a failure: turn on set -o pipefail before a critical pipeline. Seventh, for one input processed several ways, use process substitution, tee >(command); remember it is asynchronous, so wait before reading the result, and start the script with #!/bin/bash. Eighth, to write into a file only root can write, use echo content | sudo tee file, never sudo echo content > file.

Finally, the boundary. This guide covers only the one command tee: one input, split several ways. Pipes and redirects themselves — the vertical bar, >, >>, 2>, and exit codes — were covered in the pipes-and-redirection guide, which never once mentioned tee, and they are not repeated here. tee never changes the content of its input; it only copies it, so what comes in goes out unchanged, only to more places. Test record: every step above was run for real on GNU coreutils 9.4 and bash 5.2.21, and the full output is in demo279_tee.sh and demo279_tee_transcript.txt (106 lines). When data flows through a pipe, splitting off a copy to keep costs one command, and saves running the whole thing a second time.

Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles

Real photograph of a wooden desk with a white alarm clock beside a calendar page (illustrative photo)
Guide
crontab From Zero: Jobs That Just Run on Time, Five Fields Explained Once

cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.

Developer WorkflowAutomationTool Tips
Real photograph of a server room patch panel filled with blue network cables (illustrative photo)
Guide
ping From Zero: One Packet Tells You If It Is Alive — the Exit Code Matters More Than the Output

ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.

Developer WorkflowTool TipsDebugging
Real photograph of scissors cutting a white sheet of paper on a white background (illustrative photo)
Guide
cut From Zero: Slicing Columns Out of Lines

cut does one thing: split each line into fields on a delimiter and output the fields you ask for. This guide runs every command for real on GNU coreutils 9.4 — field lists and ranges, CSV and output delimiters, character slicing, --complement, the -s rule for lines without a delimiter, log-column statistics, and the quoted-comma limit — with the output pasted verbatim.

Developer WorkflowTool TipsAI Coding