Lesson 7: Connecting commands — pipe | and redirect >
We can already run single commands; the real power of the command line is connecting them. We'll meet two connectors: the pipe | takes one command's output and feeds it as input to the next — e.g. cat app.log | grep ERROR; and redirect > sends the output to a file instead of the screen — grep ERROR
The pipe | is a conveyor belt: what comes out of one command goes straight into the next. > pours the output into a file instead of onto the screen, and >> adds to the end of the file without erasing what was already there.
- pipe (|)
- Sends the output of the command on its left as input to the command on its right. E.g. cat app.log | grep ERROR — cat's output goes into grep.
- redirect (>)
- Sends the output to a file instead of the screen. grep ERROR app.log > errors.txt creates/overwrites errors.txt with the result. Nothing prints to the screen.
- append (>>)
- Like > but adds to the end of the file instead of overwriting it — handy to accumulate lines over time without losing the earlier ones.