Part 2 · 1 chapters · ~8 min
The Unix Philosophy
Small programs that do one thing, composition with pipes, text streams as a universal interface, everything is a file, exit codes and stderr, filters, designing your own CLIs to compose (stdin, stdout, --json output, exit codes), and how the philosophy shows up in modern systems.
3
Composition in practice
code
# the ten most common words in a file: five small tools
tr -cs 'A-Za-z' '\n' < README.md | tr 'A-Z' 'a-z' | sort | uniq -c | sort -rn | head
# which client IPs hit 5xx most in an access log
awk '$9 ~ /^5/ {print $1}' access.log | sort | uniq -c | sort -rn | head
# a CLI that composes: data to stdout, logs to stderr, exit codes, machine-readable output
mytool export --json 2>/dev/null | jq -r '.[] | select(.state == "failed") | .id' | xargs -n1 mytool retry
echo $? # 0 if every retry succeededDesigning your own tools: read stdin when no file is given, write results to stdout and messages to stderr, offer --json, return meaningful exit codes, and stay quiet on success. Tools built this way compose with everything else for free.
THE UNIX PHILOSOPHY
Doug McIlroy's summary, and what it means now
swipe the figure sideways, or tap expand for full screen
1/4
small tools
McIlroy: write programs that do one thing and do it well, and that work together. A word-frequency count is tr | sort | uniq -c | sort -rn | head.
compose small toolspipes as glue