Skip to content

Pipelines and tee

Module
Input, Output and Pipes
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
Prerequisites
On this page

You need the final entries of a log, a saved copy of those entries, and their line count. Writing a temporary file between every command would work, but each program can already print text and read text. You need a connection between them.

By the end, you can build that connection one stage at a time, inspect what each stage passes onward, and save a copy without interrupting the flow.

Finish Redirection (B09.2). Create this small synthetic log only in ~/lab/b09/pipelines:

Terminal window
mkdir -p ~/lab/b09/pipelines
cd ~/lab/b09/pipelines
printf 'check 1: ready\n' > events.log
printf 'check 2: reading\n' >> events.log
printf 'check 3: waiting\n' >> events.log
printf 'check 4: reading\n' >> events.log
printf 'check 5: complete\n' >> events.log

The numbers label fixed sample entries, not real events or times. Start with the replacing write, then append the remaining lines. Use Reset this lesson before repeating the setup.

Read the fixture:

Terminal window
cat events.log
Captured output
check 1: ready
check 2: reading
check 3: waiting
check 4: reading
check 5: complete

cat opens events.log and writes its text to stdout. B07.1 used head and tail with filenames to select the beginning or end of a file. Without a filename, these programs can read stdin instead.

Connect cat to tail:

Terminal window
cat events.log | tail -n 3
Captured output
check 3: waiting
check 4: reading
check 5: complete

The vertical bar, |, creates a pipe. Bash connects the command on its left’s stdout to the command on its right’s stdin. A pipeline is the sequence of commands joined by these connections.

Here cat sends five lines into the pipe. tail reads those lines from stdin and writes only the last three to its own stdout. That final stdout still reaches the terminal. The pipe carries the text, rather than passing events.log as a filename argument to tail.

For this selection alone, tail -n 3 events.log is shorter. The expanded form makes the connection visible before you add more stages. A pipe itself does not require an intermediate file.

tee reads stdin and copies it both to stdout and to each file named as an argument. Its name refers to that branching connection.

Predict first: does adding tee recent.log remove the three lines from the terminal?

Terminal window
cat events.log | tail -n 3 | tee recent.log
Run it, then compare with your guess
Captured output
check 3: waiting
check 4: reading
check 5: complete

No. tee saves those lines in recent.log and also writes them onward through stdout. With no following pipe or redirection, that copy reaches the terminal.

tee replaces an existing destination file by default. This example deliberately refreshes recent.log with the selected three lines. Its -a option appends instead, as documented in man tee; these examples use replacement.

The destination filename is an argument to tee. That differs from > recent.log, which Bash handles without passing the filename as an argument to the preceding command. Both can save text, but a simple > redirects stdout away; tee also supplies stdout for another stage.

Predict first: will the next command print five lines, three lines, or only a number? Which text will recent.log contain?

Terminal window
cat events.log | tail -n 3 | tee recent.log | wc -l
Run it, then compare with your guess
Captured output
3

wc -l, from the B04 practice and B07, counts newlines. It receives the three lines from tee, rather than the original five. With no filename argument, it reads stdin and prints the count without a filename beside it.

The file still contains text, not that count:

Terminal window
cat recent.log
Captured output
check 3: waiting
check 4: reading
check 5: complete

tee sits before wc, so its saved copy is the input to the count. Moving a saving stage changes which text it receives.

Build the pipeline one stage at a time
learner@rootvolt:~/lab$ cd ~/lab/b09/pipelineslearner@rootvolt:~/lab/b09/pipelines$ cat events.logcheck 1: ready
check 2: reading
check 3: waiting
check 4: reading
check 5: completelearner@rootvolt:~/lab/b09/pipelines$ cat events.log | tail -n 3check 3: waiting
check 4: reading
check 5: completelearner@rootvolt:~/lab/b09/pipelines$ cat events.log | tail -n 3 | tee recent.logcheck 3: waiting
check 4: reading
check 5: completelearner@rootvolt:~/lab/b09/pipelines$ cat events.log | tail -n 3 | tee recent.log | wc -l3learner@rootvolt:~/lab/b09/pipelines$ 

Contents text of the file

recent.log~/lab/b09/pipelines/
  1. check 3: waiting
  2. check 4: reading
  3. check 5: complete

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Each cumulative command reveals the text after another stage: five lines, three lines, a saved copy, then the count 3.
Steps (14)
  1. Type cd ~/lab/b09/pipelines
  2. Press Enter
  3. Type cat events.log
  4. Press Enter
  5. Output: check 1: ready check 2: reading check 3: waiting check 4: reading check 5: complete
  6. Type cat events.log | tail -n 3
  7. Press Enter
  8. Output: check 3: waiting check 4: reading check 5: complete
  9. Type cat events.log | tail -n 3 | tee recent.log
  10. Press Enter
  11. Output: check 3: waiting check 4: reading check 5: complete
  12. Type cat events.log | tail -n 3 | tee recent.log | wc -l
  13. Press Enter
  14. Output: 3

The replay builds the pipeline through separate runs so each intermediate result is visible. Its Contents panel follows only recent.log: the first tee creates its three-line copy, then the complete pipeline writes the same text again. That identical rewrite gets no changed or added marks. Watch the panel retain those three lines while the terminal prints only 3. In the complete pipeline, those intermediate stdout streams enter pipes; only wc’s stdout reaches the terminal.

Select, save, and count three log lines cat sends five lines to tail, which passes the last three to tee. tee writes those three lines to recent.log and to wc, which prints the count 3. cat events.log stdout to stdin tail -n 3 stdout to stdin tee recent.log stdout to stdin wc -l recent.log stdout: 3
Select, save, and count three log lines cat sends five lines to tail, which passes the last three to tee. tee writes those three lines to recent.log and to wc, which prints the count 3. cat events.log stdout to stdin tail -n 3 stdout to stdin tee recent.log stdout to stdin wc -l recent.log stdout: 3
Simplified model. cat supplies five lines, tail selects the last three, tee saves and passes those three, and wc counts them.

In words: cat events.log sends stdout to tail -n 3’s stdin. tail sends the last three lines to tee recent.log. tee copies those lines into recent.log and sends the same text to wc -l’s stdin. wc prints 3 to the terminal. This model shows data paths, omitting scheduling and buffering inside the programs.

An ordinary | connects stdout only. Try a file that does not exist:

Terminal window
cat missing.log | wc -l
Captured output
cat: missing.log: No such file or directory
0

cat sends its diagnostic to stderr, which still reaches the terminal. It sends no file text to stdout, so wc receives no newlines and prints 0. The diagnostic was visible, but it was not counted. Other programs and longer operations can display their streams in a different relative order.

B04.3 connected the help output of ls to grep. The pipe had this same role: stdout from the left became stdin for the right. B10 teaches the text-search command; this lesson selects log entries by position using commands you already know.

Assuming every stage prints in the terminal. Intermediate stdout enters the next pipe. Build the command progressively, as in the replay, when you need to inspect that text.

Counting a visible diagnostic as input. Stderr does not enter an ordinary pipe. The missing-file example prints a diagnostic and still gives wc no lines to count.

Saving over the source. Keep recent.log separate from events.log. A program that opens a destination for replacement can empty it while another stage is trying to read it.

Goal: Save the first two entries of events.log in first.log while printing only their newline count. Then inspect the saved file to prove which entries you selected. Preserve the source log.

Hint

Choose head for the beginning. Put the copying stage before the counting stage.

Solution
Terminal window
cat events.log | head -n 2 | tee first.log | wc -l
Captured output
2
Terminal window
cat first.log
Captured output
check 1: ready
check 2: reading

The count describes the selected stream; first.log preserves that stream for inspection. The pipeline does not modify events.log.

Terminal window
cd ~/lab/b09
ls -AR pipelines
Terminal window
rm -r ~/lab/b09/pipelines

Rerun Before you start. If the directory is already absent, skip removal.

  • | connects one command’s stdout to the next command’s stdin; it carries text, not filename arguments.
  • Build a pipeline stage by stage to inspect what each stage passes onward.
  • tee copies stdin to a named file and to stdout, allowing the stream to continue.
  • An ordinary pipe leaves stderr connected to its existing destination.

Next, Exit status and conditional execution checks command results even when they print nothing.