Pipelines and tee
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.
Before you start
Section titled “Before you start”Finish Redirection (B09.2). Create this small synthetic log only in ~/lab/b09/pipelines:
mkdir -p ~/lab/b09/pipelinescd ~/lab/b09/pipelinesprintf 'check 1: ready\n' > events.logprintf 'check 2: reading\n' >> events.logprintf 'check 3: waiting\n' >> events.logprintf 'check 4: reading\n' >> events.logprintf 'check 5: complete\n' >> events.logThe 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.
Start with the full stream
Section titled “Start with the full stream”Read the fixture:
cat events.logcheck 1: readycheck 2: readingcheck 3: waitingcheck 4: readingcheck 5: completecat 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:
cat events.log | tail -n 3check 3: waitingcheck 4: readingcheck 5: completeThe 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.
Follow the text across another connection
Section titled “Follow the text across another connection”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?
cat events.log | tail -n 3 | tee recent.logRun it, then compare with your guess
check 3: waitingcheck 4: readingcheck 5: completeNo. 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.
Count what reaches the final program
Section titled “Count what reaches the final program”Predict first: will the next command print five lines, three lines, or only a number? Which text will recent.log contain?
cat events.log | tail -n 3 | tee recent.log | wc -lRun it, then compare with your guess
3wc -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:
cat recent.logcheck 3: waitingcheck 4: readingcheck 5: completetee sits before wc, so its saved copy is the input to the count. Moving a saving stage changes which text it receives.
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
check 3: waitingcheck 4: readingcheck 5: complete
Steps (14)
- Type
cd ~/lab/b09/pipelines - Press Enter
- Type
cat events.log - Press Enter
- Output:
check 1: ready check 2: reading check 3: waiting check 4: reading check 5: complete - Type
cat events.log | tail -n 3 - Press Enter
- Output:
check 3: waiting check 4: reading check 5: complete - Type
cat events.log | tail -n 3 | tee recent.log - Press Enter
- Output:
check 3: waiting check 4: reading check 5: complete - Type
cat events.log | tail -n 3 | tee recent.log | wc -l - Press Enter
- 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.
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.
Keep diagnostics out of the pipe
Section titled “Keep diagnostics out of the pipe”An ordinary | connects stdout only. Try a file that does not exist:
cat missing.log | wc -lcat: missing.log: No such file or directory0cat 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.
Mistakes to recognize
Section titled “Mistakes to recognize”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.
Try it yourself
Section titled “Try it yourself”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
cat events.log | head -n 2 | tee first.log | wc -l2cat first.logcheck 1: readycheck 2: readingThe count describes the selected stream; first.log preserves that stream for inspection. The pipeline does not modify events.log.
Reset this lesson
Section titled “Reset this lesson”cd ~/lab/b09ls -AR pipelinesrm -r ~/lab/b09/pipelinesRerun 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.
teecopies 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.
Further reading
Section titled “Further reading”- On your system:
man bash,man tee,man head,man tail, andman wc. - GNU Bash: Pipelines.
- GNU Coreutils: tee invocation.
- Ubuntu 24.04: tee(1), tail(1), and wc(1).