Linux Pipes Explained: 5 Practical Examples
Linux pipes let you send the output of one command into another command with the | symbol. That is the whole trick: instead of saving a temporary file, opening another tool, and copying data around, you can build a quick command chain like ps aux | grep ssh or cat access.log | grep 500 | wc -l.
If you are new to Linux, pipes are one of the first command-line ideas worth learning because they show up everywhere: reading logs, filtering process lists, counting lines, sorting output, and troubleshooting tickets faster. This guide gives you the practical version: what pipes do, the basic syntax, useful examples, and the mistakes that make pipeline commands confusing.
What Are Linux Pipes?
In Linux, a pipe is a form of redirection that connects the output (stdout) of one command to the input (stdin) of another command. Pipes are represented by the vertical bar | symbol.
Use a pipe when the first command produces text and the second command can read text. That usually means commands such as grep, sort, uniq, wc, head, tail, awk, and sed. You can chain several commands together when each step has one simple job.
Basic Syntax of Linux Pipes
The basic syntax of a Linux pipe is as follows:
command1 | command2
-
command1: The first command in the pipeline. Its output will be passed to the next command as input. -
command2: The second command in the pipeline. It receives the output of the first command as its input.
You can extend this syntax to include multiple commands in the pipeline:
command1 | command2 | command3 | ... | commandN
Practical Examples of Using Linux Pipes
Letâs explore some practical examples of how Linux pipes can be used:
Example 1: Filtering and Sorting Directory Contents
Suppose you want to list the contents of a directory, filter the results to show only text files, and then sort the results alphabetically. You can achieve this using a combination of the ls, grep, and sort commands with pipes:
ls | grep '\.txt$' | sort
-
ls: Lists the contents of the current directory. -
grep '\.txt$': Filters the results to show only files with the.txtextension. -
sort: Sorts the filtered results alphabetically.
Example 2: Counting the Number of Lines in a File
To count the number of lines in a file, you can use the wc command with the -l option. However, if you want to exclude blank lines from the count, you can use a pipe with the grep command:
grep -v '^$' file.txt | wc -l
-
grep -v '^$' file.txt: Filters out blank lines from the file âfile.txt.â -
wc -l: Counts the number of lines in the filtered output.
Example 3: Monitoring System Logs in Real-Time
You can use pipes to monitor system logs in real-time and filter the output for specific keywords. For example, to monitor the system log for occurrences of the word âerror,â you can use the tail and grep commands with a pipe:
sudo tail -f /var/log/syslog | grep -i 'error'
-
sudo tail -f /var/log/syslog: Monitors the system log in real-time. -
grep -i 'error': Filters the output to show only lines containing the word âerrorâ (case-insensitive).
Example 4: Find the Noisiest Processes
When a Linux box feels slow, pipes help you narrow the output instead of staring at a giant process list:
ps aux | sort -nrk 3 | head -5
That sorts running processes by CPU usage and shows the top five. For memory instead, sort by the fourth column:
ps aux | sort -nrk 4 | head -5
This is not a replacement for top or htop, but it is fast, scriptable, and easy to paste into a ticket note.
Common Pipe Mistakes
The most common beginner mistake is piping commands that do not produce useful text output. Pipes work best when the command on the left prints lines and the command on the right expects lines. Another mistake is making one giant pipeline before testing the pieces. Build it one command at a time: run ps aux, then add | sort -nrk 3, then add | head -5.
Also watch for commands that write errors to stderr instead of stdout. If a pipeline seems to âmissâ errors, that may be why. Learn the simple case first; then learn stderr redirection when you actually need it.
Conclusion
Linux pipes are an essential tool for anyone working with the command line
. They provide a simple yet powerful way to chain commands together, allowing you to build complex data-processing pipelines with ease. By mastering the use of pipes, you can enhance your productivity, automate repetitive tasks, and unlock the full potential of the Linux command-line environment.
In this blog post, we explored the basics of Linux pipes, demonstrated their use in practical examples, and showcased their versatility in solving real-world problems. Whether youâre a system administrator, developer, or Linux enthusiast, understanding and using pipes is a valuable skill that will serve you well in your Linux journey.
We hope you found this tutorial helpful and that youâre now more confident in using Linux pipes to streamline your command-line workflows. As you continue to explore the Linux command-line environment, youâll discover a wide range of tools and commands that can be combined with pipes to achieve powerful results. Keep experimenting, learning, and building your command-line expertise. Happy piping!
Practice this in Shell Samurai
Reading the command is useful. Running it until it feels boring is better. Practice Linux in Shell Samurai so you can make mistakes in a safe terminal instead of on a real ticket.
Practice Linux commands next
If you want these commands to actually stick, do not stop at reading:
- Start Shell Samurai Chapter 1 free and practice in a real terminal.
- Use the Linux commands cheat sheet as your quick reference.
- Browse more Shell Samurai field notes after you finish a mission.
- Comparing resources? See the Linux Journey alternative breakdown.
Practice This in a Real Terminal
Shell Samurai gives you safe Linux missions so the commands actually stick. Chapter 1 is free; the full practice path is a one-time purchase, not another subscription.