Skip to content

Pipes (|) in Linux

Pipes are one of the most powerful features of Linux. They allow you to connect multiple commands together, where the output of one command becomes the input of another. Instead of creating temporary files, you can build efficient command pipelines for filtering, searching, analyzing, and processing data.


Learning Path

Linux Mastery → Module 2: Linux Command Line Essentials → Lesson 10

Difficulty: Beginner → Intermediate

Reading Time: 30 Minutes

Course Progress

Course: Linux Mastery

Module: Linux Command Line Essentials

Lesson: 10 of 10


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand what a pipe is
  • Learn how pipes work internally
  • Connect multiple Linux commands
  • Filter and process command output
  • Build powerful command pipelines
  • Use pipes in real-world administration tasks
  • Apply pipes in DevOps and Cloud environments

Prerequisites

Before starting this lesson, complete:

  • Module 1 – Linux Fundamentals
  • Module 2 Lessons 1–9

Why Learn Pipes?

Suppose you have 5,000 running processes.

You don't want to see all of them.

Instead, you only want to find:

  • Docker
  • Kubernetes
  • NGINX
  • SSH

Without pipes, this becomes difficult.

With pipes:

ps -ef | grep nginx

One command.

Instant results.


What is a Pipe?

A pipe (|) sends the Standard Output (stdout) of one command directly to the Standard Input (stdin) of another command.

Instead of displaying output on the screen, Linux transfers it directly between commands.


How Pipes Work

Command 1


Standard Output


Pipe (|)


Standard Input


Command 2


Output

Example:

ls | wc -l

Workflow:

List Files


Pipe


Count Files


Display Count

Basic Pipe Example

Without pipe:

ls

Displays:

Documents

Downloads

Pictures

Videos

With pipe:

ls | wc -l

Output:

4

Pipe with grep

Find running SSH processes.

ps -ef | grep ssh

Pipeline:

ps -ef


grep ssh


Matching Processes

Pipe with head

Display only the first five files.

ls -l | head -5

Pipe with tail

Display the last five files.

ls -l | tail -5

Multiple Pipes

Linux allows unlimited pipelines.

Example:

cat users.txt | sort | uniq

Workflow:

Read File


Sort


Remove Duplicates


Display Result

Count Matching Results

Example:

cat users.txt | grep admin | wc -l

Pipeline:

Read File


Find "admin"


Count Matches


Display Total

Sort Output

Example:

cat employees.txt | sort

Output:

Alice

Bob

Charlie

David

Reverse sort:

cat employees.txt | sort -r

Remove Duplicate Entries

Example:

sort employees.txt | uniq

Count duplicates:

sort employees.txt | uniq -c

Example:

2 Alice

1 Bob

1 Charlie

Pipe with tee

Display output while saving it.

ls | tee files.txt

Output:

Documents

Downloads

Pictures

The same output is saved to:

files.txt

Pipe with less

Large output:

journalctl | less

Benefits:

  • Scroll
  • Search
  • Navigate

Ideal for production logs.


Pipe with xargs

Suppose you have:

file1.txt

file2.txt

file3.txt

Delete them:

find . -name "*.txt" | xargs rm

Warning

Use carefully.

Preview first:

find . -name "*.txt"

Complex Pipeline Example

Display running Docker containers.

docker ps | grep nginx | wc -l

Workflow:

List Containers


Find nginx


Count Matches

Another Example

Count running Kubernetes Pods.

kubectl get pods | grep Running | wc -l

Real Production Examples

Find failed services.

systemctl --failed | less

Search logs.

journalctl | grep ERROR

Find listening ports.

ss -tuln | grep 443

Display top memory consumers.

ps aux | sort -rk4 | head -10

Count active users.

who | wc -l

Find SSH login attempts.

journalctl | grep ssh

Visual Pipeline

Application Logs


grep ERROR


sort


uniq


wc -l


Total Errors

Command Summary

Pipeline Purpose
ls \| wc -l Count files
ps -ef \| grep ssh Search process
cat file \| sort Sort data
sort file \| uniq Remove duplicates
history \| grep docker Search history
journalctl \| less Read logs
find . \| xargs Process search results

Production Perspective

Pipes are everywhere.

Docker

docker ps | grep redis

Kubernetes

kubectl get pods | grep Running

Git

git log | head

Linux

ps aux | grep nginx

Networking

ss -tuln | grep 22

Monitoring

journalctl | grep CRITICAL

Every DevOps engineer uses pipelines daily.


Hands-on Lab

Task 1

Count files.

ls | wc -l

Task 2

Display first five entries.

ls -l | head -5

Task 3

Display last five entries.

ls -l | tail -5

Task 4

Create sample file.

cat > employees.txt

Add:

Alice

Bob

Charlie

Alice

David

Press:

Ctrl + D

Task 5

Sort.

cat employees.txt | sort

Task 6

Remove duplicates.

sort employees.txt | uniq

Task 7

Count duplicates.

sort employees.txt | uniq -c

Task 8

Search history.

history | grep ls

Task 9

Save and display output.

ls | tee files.txt

Production Troubleshooting Scenario

Scenario

An NGINX server is experiencing intermittent failures.

Tasks:

  1. Find the NGINX process.
  2. Display only error logs.
  3. Count the number of error entries.
  4. Save filtered errors to a file.
  5. Display and save the output simultaneously.

Solutions:

ps -ef | grep nginx

journalctl | grep ERROR

journalctl | grep ERROR | wc -l

journalctl | grep ERROR > errors.log

journalctl | grep ERROR | tee errors.log

Mini Challenge

Create a file named:

servers.txt

Contents:

server01

server03

server02

server01

server04

server02

Now:

  • Sort the file.
  • Remove duplicates.
  • Count unique servers.
  • Save sorted output.
  • Display the first three entries.
  • Display the last two entries.

Use only pipes and commands you've learned.


Best Practices

  • Use pipes instead of temporary files whenever possible.
  • Keep pipelines readable.
  • Test each command individually before combining them.
  • Use tee when you need to both save and view output.
  • Filter data early to improve performance.

Common Mistakes

❌ Creating unnecessary temporary files.

✅ Instead of:

ls > files.txt

cat files.txt

Use:

ls | less

❌ Building very long pipelines without testing.

✅ Test each stage before combining them.


❌ Forgetting that pipes transfer only stdout.

✅ If you also need stderr, redirect it appropriately.


Interview Questions

Beginner

  1. What is a pipe?
  2. What does the | symbol do?
  3. What is the difference between a pipe and redirection?
  4. Which command counts lines?

Intermediate

  1. Explain how pipes work internally.
  2. Why are pipes preferred over temporary files?
  3. Explain sort | uniq.
  4. What does tee do in a pipeline?

Architect Level

  1. How do pipes simplify Linux automation?
  2. How would you analyze millions of log entries using pipelines?
  3. Why are pipelines fundamental to DevOps workflows?

Summary

In this lesson, you learned:

  • What pipes are
  • How pipelines work
  • Combining Linux commands
  • Filtering and processing command output
  • Using sort, uniq, wc, grep, head, tail, and tee with pipes
  • Production-ready command pipelines

Pipes are one of Linux's most elegant features. By combining simple commands, you can perform complex tasks efficiently without writing custom programs.


Key Takeaways

  • Pipes connect the output of one command to the input of another.
  • Pipelines eliminate the need for temporary files.
  • Combining simple commands creates powerful workflows.
  • Pipes are heavily used in Linux administration, DevOps, and Cloud Engineering.
  • Mastering pipelines significantly improves productivity and troubleshooting skills.

What's Next?

Module 2 Summary — Linux Command Line Essentials

In the next lesson, you'll:

  • Review all key concepts from Module 2
  • Complete a hands-on assessment
  • Test your knowledge with quizzes
  • Solve production-focused challenges
  • Prepare for Module 3: Text Processing