Building Pipelines
A single pipe is useful. A pipeline of three or more commands is where Linux really shines. Each command does one job and passes its result to the next.
cat access.log | grep "denied" | wc -l
3
This pipeline: (1) reads the file, (2) keeps only "denied" lines, (3) counts them.
Sorting inside a pipeline
cat names.txt | sort | head -3
This outputs the first 3 names in alphabetical order: sort reorders, head trims.
Building step by step
The best way to build a pipeline is incrementally:
- Start with the data source:
cat file - Add one command, check the output.
- Add the next command, check again.
If something goes wrong, remove the last pipe and inspect the intermediate output.
Common pipeline commands
| Command | Role in a pipeline |
|---|---|
grep | Filter lines |
sort | Reorder lines |
wc -l | Count lines |
head / tail | Take first/last N lines |
Mission: Priority Alert Briefing
The bridge crew needs a quick briefing: only the two most urgent alerts from today's event log, sorted alphabetically so they can cross-reference the duty roster.
- Build a pipeline that reads
events.log, keeps only the lines containingalert, sorts them alphabetically, and shows only the first 2 results. Chain at least 3 pipes. - Send that two-line briefing into a file called
briefing.txt. - Create a folder called
bridgeand movebriefing.txtinto it. - In a separate pipeline, count how many
alertlines the full log contains.
Expected result
bridge/briefing.txt holds the two sorted alert lines.