This is where the shell becomes powerful. The Unix philosophy is to build small tools that each do one thing well, then combine them. Pipes are the glue — the output of one command becomes the input of the next — and with them you'll process text and data in one line that would take a whole script elsewhere.
The Unix philosophy
Each command reads input, does one job, and writes output. Because they all speak plain text, any command's output can feed any other's input. Master a handful of small tools and you can combine them endlessly.
Redirection
By default, output goes to the screen. Redirect it to a file instead:
ls > files.txt # write output to a file (overwrites)ls >> files.txt # append to a filesort < names.txt # feed a file in as inputcommand 2> err.log # redirect errors (stderr) to a file
Pipes
The pipe | connects commands. Read it left-to-right as "then": list the files, then search them, then count:
# Count how many .log files are herels | grep ".log" | wc -l# Find the 5 largest processes by memoryps aux | sort -k4 -r | head -5# Unique programmes, sorted, in a CSV columncat students.csv | cut -d, -f2 | sort | uniq -c
Text tools
| Tool | Does |
|---|---|
grep | Find lines matching a pattern |
cut | Extract columns/fields |
sort | Sort lines |
uniq | Collapse or count duplicates |
wc | Count lines, words, characters |
head / tail | First / last N lines |
sed | Find-and-replace / stream edit |
awk | Field-based processing & sums |
grep -i "error" app.log # case-insensitive searchgrep -rn "TODO" src/ # recursive, with line numberssed 's/old/new/g' file.txt # replace every "old" with "new"awk -F, '{ sum += $3 } END { print sum }' data.csv # sum column 3
Practice
Given a CSV, build a one-liner pipeline that extracts one column, sorts it, and counts how many times each value appears (cut | sort | uniq -c). Then use grep to find all error lines in a log and wc -l to count them.
Key takeaways
- 01The Unix philosophy: small single-purpose tools that combine via plain text.
- 02Redirect output with > (overwrite) and >> (append); feed input with <.
- 03Pipes (|) chain commands — one's output becomes the next's input.
- 04Core text tools: grep, cut, sort, uniq, wc, head/tail, sed, awk.
- 05tail -f follows logs live; pipe into grep to filter to what matters.
