GICT knowledge base
How to Chain Linux commands together: Five pipes that save time every day.
Published
Most Linux beginners run one command at a time. You type, you hit enter, you read the output, you type the next command. It works, but it is slow. There is a better way.
Linux pipes let you chain commands together so the output of one becomes the input of the next. Once you see how this works, you will start combining commands instinctively. Tasks that used to take five steps take one.
What a Pipe Actually Does
The pipe symbol, which you type as |, connects two commands. Everything the command on the left prints to the screen gets fed directly into the command on the right, without creating a temporary file in between.
Think of it like a factory line. One machine finishes a part and passes it straight to the next machine. No box in between, no waiting for someone to carry it.
The shell processes everything left to right. You can chain as many commands as you want with multiple pipes.
Your First Pipe
Open a terminal and try this:
ls -la /var/log | grep error
This lists everything in /var/log, then passes that list to grep, which filters for lines containing the word “error”. Without the pipe, you would see the full list, then have to scroll through it manually to find errors.
Here is another one that comes up constantly:
ps aux | grep nginx
This shows every running process, then narrows it down to just the nginx ones. The ps aux command alone shows everything. Adding | grep nginx at the end cuts through the noise instantly.
Pipes That Save Time Every Day
Find the Biggest Files Fast
Running out of disk space is one of those problems that sneaks up on you. When you need to track down what is eating your storage, this pipe does the heavy lifting:
du -h /home | sort -rh | head -10
Let me break that down. du -h /home estimates disk usage for everything under /home and prints sizes in human-readable format (MB, GB). The sort -rh command sorts those results numerically, biggest first. head -10 grabs the top 10 lines. The result is a ranked list of your largest directories, in under a second.
Watch Logs in Real Time
When you are debugging something live, watching a log file scroll is often easier than searching through old entries. This is how you do it:
tail -f /var/log/syslog | grep sshd
The tail -f command prints new lines as they are added to the file. Adding | grep sshd filters those lines so you only see SSH-related entries. This is how you watch for login attempts in real time.
Count Lines Without Opening a File
How many lines are in that access log? Pipe the output to wc -l and it counts them for you:
cat /var/log/nginx/access.log | wc -l
The wc command stands for word count. The -l flag switches it to count lines instead. This works on any file, any size.
Common Patterns Worth Memorizing
Once you start using pipes, you will notice a few combinations that show up constantly. Here are the ones worth knowing by heart.
Search inside output. Add | grep pattern to filter anything for a specific word or phrase. grep -i makes the search case-insensitive, so it catches Error, ERROR, and error alike.
Sort before you display. | sort puts lines in alphabetical order. Add the -r flag to reverse it, or -n to sort numerically. This is useful when you want the largest files, fastest times, or highest numbers at the top.
Remove duplicate lines. | uniq collapses repeated lines into one. This is handy after a sort | uniq combination when you need a clean list of unique values from a dataset.
Count what you see. | wc -l counts lines. | wc -w counts words. These come in handy when you are doing quick forensics on logs or output files.
When Pipes Misbehave
Sometimes a pipe does not give you what you expect. Most of the time the problem is stderr versus stdout.
Linux has two output streams. Standard output (stdout) is where normal output goes. Standard error (stderr) is where error messages go. By default, pipes only carry stdout. Error messages print to the screen and bypass the pipe entirely.
When you need the error messages to flow through the pipe too, add 2>&1 at the end of your command chain. This tells the shell to redirect stderr into stdout first:
somecommand arg1 arg2 2>&1 | grep error
The 2>&1 part reads as “redirect file descriptor 2 (stderr) to the same place as file descriptor 1 (stdout).” Once they are merged, the pipe carries both.
Three Pipes to Try Right Now
Here are three combinations worth putting into muscle memory. Open a terminal and run each one.
First, find your top 5 largest packages installed via apt:
dpkg-query --show --format='${Installed-Size}\t${Package}\n' | sort -rn | head -5
Next, see your ten most recent login attempts:
last | head -10
Finally, find out how many distinct IP addresses have accessed your server:
cat /var/log/nginx/access.log | awk '{print $1}' | sort -u | wc -l
The awk command pulls out the first column (the IP address). sort -u removes duplicates, and wc -l counts what remains.
Pipes Are a Habit
You do not need to memorize every possible combination. You need to understand the pattern: take output, filter it, sort it, count it, or transform it with the next command in the chain. Once that clicks, you will start building pipes on the fly without planning them ahead.
The examples above are just starting points. Take any command that prints a list, then think about what you actually want to do with that list. Filter it, sort it, count it, or find something specific in it. That is usually a pipe away.
Related guides
How to Set Up zram on a Small VPS
Configure zram on Ubuntu with kernel and swap checks. Understand compressed memory limits, monitor real use and avoid unsafe swap-file changes.
LinuxHow to Find Services That Need a Restart After Updates with needrestart
Find services using outdated libraries with needrestart on Ubuntu. Read restart reports, review automatic restart policy and plan maintenance safely.
LinuxHow to Limit a Linux Service’s Memory with systemd
Limit a Linux service with systemd MemoryHigh, MemoryMax and swap controls. Check cgroup support, diagnose pressure and remove only your own policy.
LinuxHow to Protect SSH with CrowdSec on Ubuntu
Protect SSH with CrowdSec on Ubuntu. Connect log detection to firewall enforcement and test temporary blocking without losing administrator access.
