Skip to main content

Command Palette

Search for a command to run...

Linux Pipes and Redirects — Connecting Commands Together

Published
4 min readView as Markdown

So far, we’ve learned many Linux commands for different tasks:

  • Listing directory contents

  • Creating users

  • Managing groups

  • Working with files and permissions

Now it’s time to learn one of the most powerful ideas in Linux the ability to connect commands together.

In Linux, every command has input and output.
And Linux gives us a way to pass the output of one command directly as the input of another.

This concept completely changes how you work on the command line.


The Core Idea: Commands Can Talk to Each Other

Instead of running commands in isolation, Linux allows you to:

  • Run one command

  • Take its output

  • Feed that output into another command

This is called piping.

The output of one program becomes the input of another program.


Example: Viewing Large Files More Comfortably

Let’s say we want to view the system log file:

/var/log/syslog

If we display it directly, the output is huge:

cat /var/log/syslog

The terminal floods with text, forcing us to scroll endlessly.

A Better Way: less

The less command displays content page by page, making large outputs readable.

To connect cat and less, we use the pipe character:

cat /var/log/syslog | less

What’s Happening Here?

  • cat outputs the file content

  • | (pipe) sends that output forward

  • less receives it as input and displays it interactively


Navigating Inside less

Inside less, you can:

  • Space → next page

  • b → previous page

  • q → quit

Notice something important:

  • The output is not printed directly to the terminal

  • It’s handled by less instead

That’s piping in action.


Piping Directory Listings

Some directories contain hundreds or thousands of files.

For example:

ls /usr/bin

Scrolling through that is painful.

Instead:

ls /usr/bin | less

Now you can:

  • Read page by page

  • Move forward and backward

  • Exit cleanly


Viewing Command History More Easily

Your command history grows quickly.

Instead of dumping it all at once:

history | less

You can now:

  • See your earliest commands

  • Scroll forward and backward

  • Exit when done


Filtering Output with grep

Another extremely powerful program is grep.

grep filters text based on a pattern.

Example: Find All Commands Run with sudo

history | grep sudo

Result:

  • Only commands containing sudo are shown

  • The matching word is highlighted

  • Output is printed directly to the terminal

This is great for traceability.


Searching for Phrases

If the pattern contains spaces, use quotes:

history | grep "sudo chmod"

This filters for commands that changed file permissions using sudo.


Chaining Multiple Commands Together

Pipes are not limited to two commands.

You can chain as many as you want.

Example:

history | grep sudo | less

Flow:

  1. history → produces all commands

  2. grep sudo → filters only sudo commands

  3. less → displays results page by page

Each command processes the output of the previous one.


Using grep on Files and Directories

Finding Programs in /usr/bin

Instead of listing everything:

ls /usr/bin | grep java

Or:

ls /usr/bin | grep python

This instantly tells you whether a program exists.


Searching Inside Files

For large configuration files:

grep port config.yaml

You can:

  • Find values

  • Search for keywords

  • Filter logs and configs efficiently


Redirecting Output to a File

Piping sends output to another program.

Redirecting sends output to a file.

Example: Save Filtered History

history | grep sudo > sudo_commands.txt
  • > redirects output

  • File is created (or overwritten)

  • Output becomes portable and shareable


Appending Instead of Overwriting

If you want to add to an existing file, use >>:

history | grep rm >> sudo_commands.txt

Now the file contains:

  • Existing sudo commands

  • Additional remove (rm) commands


Practical Use Case: Logs and Troubleshooting

Pipes and redirects are incredibly useful for:

  • Collecting logs

  • Filtering errors

  • Saving outputs

  • Sharing troubleshooting data

Example workflow:

  • Program prints logs

  • grep filters errors

  • Output is saved to a file

  • File is shared with a teammate


STDIN, STDOUT, and STDERR (Important Terms)

Linux formally names these streams:

  • STDIN → standard input

  • STDOUT → standard output

  • STDERR → standard error

When you pipe or redirect:

  • You usually pass STDOUT

  • Errors go through STDERR

If a file doesn’t exist, the error message is not part of normal output.

Knowing these terms helps when reading documentation.


Running Independent Commands with Semicolons

Not all commands need to share input or output.

You can run commands sequentially using ;:

clear; sleep 1; echo "Done"

Execution order:

  1. Clear screen

  2. Wait 1 second

  3. Print message

Each command runs independently.


Summary

In this lecture, you learned:

  • Commands have input and output

  • Piping (|) connects commands together

  • less improves readability of large outputs

  • grep filters text effectively

  • Output can be redirected to files using > and >>

  • STDIN, STDOUT, and STDERR are core Linux concepts

  • Semicolons allow sequential command execution

More from this blog

devops

22 posts