Shell Pipes and Redirection: >, >> and | Explained
The Unix idea behind the shell is "small tools that each do one thing, joined together". Redirection connects a command to a file; a pipe connects one command to another.
Standard streams
Every command has three streams: standard input (what it reads, normally the keyboard), standard output (results, normally the screen) and standard error (error messages, also the screen). Redirection changes where these go.
Redirection
command > filesends standard output to a file, replacing its contents.command >> fileappends to the end of the file.command < filefeeds the file to the command's standard input.command 2> errors.txtredirects standard error (stream number 2).
Pipes
The | character sends the output of one command straight into the input of the next, with no temporary file:
cat access.log | grep 404 | wc -l
reads a log, keeps the lines containing 404, and counts them. Each stage is simple; the combination answers a real question.
A toolkit of filters
grep wordkeeps lines containingword;-iignores case,-ccounts matches,-vinverts.sortsorts lines alphabetically;-nsorts numerically,-rreverses.uniqremoves adjacent duplicates, so it is almost always used aftersort;uniq -ccounts them.wc -lcounts lines.head -n 3andtail -n 3keep the first or last lines.
The example writes a small file of programming languages with printf, then answers questions about it with pipelines: how many lines mention Python, and which entries are most common.
# printf writes one language per line into a file
printf 'Python\nJavaScript\nPython\nSQL\nPython\nJavaScript\n' > langs.txt
wc -l < langs.txt
grep -c Python langs.txt
grep -v Python langs.txt
sort langs.txt | uniq
sort langs.txt | uniq -c | sort -nr | head -n 2
echo "Go" >> langs.txt
tail -n 2 langs.txt Transcript (verified by our test suite in a fresh temp folder)
$ printf 'Python\nJavaScript\nPython\nSQL\nPython\nJavaScript\n' > langs.txt
$ wc -l < langs.txt
6
$ grep -c Python langs.txt
3
$ grep -v Python langs.txt
JavaScript
SQL
JavaScript
$ sort langs.txt | uniq
JavaScript
Python
SQL
$ sort langs.txt | uniq -c | sort -nr | head -n 2
3 Python
2 JavaScript
$ echo "Go" >> langs.txt
$ tail -n 2 langs.txt
JavaScript
GoRead the last pipeline from left to right: sort the lines so duplicates sit together, count each group with uniq -c, sort those counts numerically in reverse, and keep the top two.
Why this matters for Git
Git commands are designed to work in pipelines too. git log --oneline | head -n 5 shows the five most recent commits, and git branch | grep feature finds feature branches. Once pipes feel natural, the terminal stops being a place for one command at a time.
Common mistakes
- Using
>when you meant>>and wiping a file. - Expecting
uniqto remove duplicates that are not next to each other. Sort first. sort numbers.txtputting 10 before 9: add-nfor numeric order.
Summary
>replaces,>>appends,<reads,2>captures errors.|chains commands; build pipelines one stage at a time.sort | uniq -c | sort -nris the classic "most common" recipe.
Quiz: Pipes and redirection