Pipes

A pipe sends the output of one command straight into the input of the next.

Syntaxcommand1 | command2 | command3

The 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.

Data flowing through a pipeline of three commandscat log|grep ERROR|wc -lcount
Each command's output flows into the next through a pipe.

The example above reads a log, keeps only lines containing ERROR, then counts them — three tiny tools solving one real task.

Example

Example · bash
# 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 20

When 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.

Example · bash
# Count files in /etc
ls /etc | wc -l
# 187

# Find unique file extensions
ls /etc | grep -o '\.[^.]*$' | sort -u

Multi-stage pipeline

Chains three commands: du collects sizes, sort orders them descending, head takes the top 5.

Example · bash
# Find top 5 largest files in /var/log
du -sh /var/log/* | sort -rh | head -5
# 128M  /var/log/syslog
#  45M  /var/log/kern.log

Filter and transform pipeline

Builds a five-stage pipeline to count and rank visitor IPs by request frequency from a log file.

Example · bash
# 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

  • Be the first to comment on this lesson.