The pipe
|
Pass the output of one command to the input of another
How it works
| (the pipe) connects commands: what the left one prints becomes the input of the right one. cat app.log | grep ERROR prints the log and keeps only the error lines straight away.
A chain can be as long as you like: history | grep git | tail -5 shows your last five git commands. That is how simple commands add up to the result you need: each does one job, and the pipe links them.
Syntax
<command> | <command>
<command> | <command> | <command>Examples
Pipe app.log into grep and keep the lines with ERROR
cat app.log | grep ERRORsame as:
cat app.log|grep ERRORcat app.log— print the file|— pipe: output on the left → input on the rightgrep ERROR— filter lines
Show the last 20 commands from history
history | tail -20same as:
history | tail -n 20,history | tail -n20history— command history|— pass it ontail -20— last 20 lines