Git & Command Line Tutorial · Command Line Basics · Lesson 5 of 20
Pipes and Redirection
Connect commands with | so the output of one feeds the next, and send output to files with >, >> and 2>. The real power of the command line.
Pipes
A pipe | sends one command's output into the next command's input. Small tools combined this way solve big problems:
grep "ERROR" app.log | cut -d' ' -f4- | sort | uniq -c | sort -rn | head -3
Handy pipeline tools: sort (-n numbers, -r reverse), uniq -c (count repeats; needs sorted input), cut -d, -f2 (columns), tr (translate characters), awk '{print $1}' (fields), sed 's/old/new/' (replace), xargs (turn lines into arguments).
Redirection
| Syntax | Meaning |
|---|---|
> file |
Write output to a file (replaces it) |
>> file |
Append to a file |
< file |
Read input from a file |
2> errors.txt |
Write errors (stderr) to a file |
> out.txt 2>&1 |
Send both output and errors to one file |
> /dev/null |
Throw output away |
printf "apple\nbanana\napple\ncherry\nbanana\napple\n" > fruit.txt
echo "--- top fruits:"
sort fruit.txt | uniq -c | sort -rn
echo "--- as a CSV header:"
head -n 3 fruit.txt | tr '\n' ',' | sed 's/,$/\n/'
echo "first run" > log.txt # > replaces
echo "second run" >> log.txt # >> appends
cat log.txt
ls missing-file 2> errors.txt || echo "--- the error went to errors.txt:"
cat errors.txt
echo "--- word lengths:"
echo "pipes connect small tools" | tr ' ' '\n' | awk '{ print length($0), $0 }'
Output
--- top fruits:
3 apple
2 banana
1 cherry
--- as a CSV header:
apple,banana,apple
first run
second run
--- the error went to errors.txt:
ls: cannot access 'missing-file': No such file or directory
--- word lengths:
5 pipes
7 connect
5 small
5 toolsRun this on your computer — see “Get Started” for setup.
Test yourself
What does >> do?
Replaces the fileAppends output to the end of a fileReads from a filePipes to another command
Show answer
Answer: Appends output to the end of a file