Pipes
A pipe sends the output of one command straight into the input of the next.
Syntax
command1 | command2 | command3The pipe character | connects commands: the output of the command on the left becomes the input of the command on the right. This lets you build powerful chains from small, simple tools.
The example above reads a log, keeps only lines containing ERROR, then counts them — three tiny tools solving one real task.
Example
# Count how many error lines are in a log
cat app.log | grep ERROR | wc -l
# 42
# List processes, keep nginx ones
ps aux | grep nginx
# The 20 biggest items in a folder
du -h * | sort -h | tail -n 20When to use it
- A developer pipes 'cat access.log | grep 404 | wc -l' to count HTTP 404 errors in a web server log.
- A sysadmin runs 'ps aux | grep nginx | grep -v grep' to find running nginx worker processes without extra noise.
- A DevOps engineer pipes 'kubectl get pods | grep CrashLoopBackOff | awk "{print $1}"' to list crashing pod names.
More examples
Chain two commands with a pipe
Pipes ls output into wc and grep to count files and extract unique extensions without intermediate files.
# Count files in /etc
ls /etc | wc -l
# 187
# Find unique file extensions
ls /etc | grep -o '\.[^.]*$' | sort -uMulti-stage pipeline
Chains three commands: du collects sizes, sort orders them descending, head takes the top 5.
# Find top 5 largest files in /var/log
du -sh /var/log/* | sort -rh | head -5
# 128M /var/log/syslog
# 45M /var/log/kern.logFilter and transform pipeline
Builds a five-stage pipeline to count and rank visitor IPs by request frequency from a log file.
# Extract all unique IP addresses from nginx access log
awk '{print $1}' /var/log/nginx/access.log \
| sort \
| uniq -c \
| sort -rn \
| head -10
Discussion