0
0
Linux CLIscripting~5 mins

Why pipes chain commands into workflows in Linux CLI - Why It Works

Choose your learning style9 modes available
Introduction
Sometimes you want to do several small tasks one after another, where the output of one task becomes the input of the next. Pipes let you connect commands so they work together smoothly without creating extra files.
When you want to count how many files have a certain word inside them.
When you want to list running processes and find only those using a lot of memory.
When you want to see the last few lines of a log file and then search for a specific error message.
When you want to sort a list of users by their login time and then show only the top 5.
When you want to filter system information and then format it for easier reading.
Commands
This command lists all running processes and then filters the list to show only those related to 'firefox'. The pipe '|' sends the output of 'ps aux' directly into 'grep firefox'.
Terminal
ps aux | grep firefox
Expected OutputExpected
user 1234 0.0 1.2 123456 7890 ? S 10:00 0:00 /usr/lib/firefox/firefox user 5678 0.0 0.5 654321 4321 ? S 10:05 0:00 /usr/lib/firefox/firefox -contentproc
This command reads the system log file, takes the last 20 lines, and then filters those lines to show only ones containing the word 'error'. Each pipe passes the output to the next command.
Terminal
cat /var/log/syslog | tail -n 20 | grep error
Expected OutputExpected
Jun 10 10:15:01 server systemd[1]: error: Failed to start service Jun 10 10:15:05 server kernel: error: Disk read failure
-n 20 - Shows only the last 20 lines from the input
This command lists files in /usr/bin with details, sorts them by file size (5th column) in numeric order, and then shows the 3 largest files. Pipes chain these commands to create a workflow.
Terminal
ls -l /usr/bin | sort -k5 -n | tail -n 3
Expected OutputExpected
-rwxr-xr-x 1 root root 1234 Jan 1 12:00 file1 -rwxr-xr-x 1 root root 2345 Jan 1 12:01 file2 -rwxr-xr-x 1 root root 3456 Jan 1 12:02 file3
-l - Lists files with detailed information
-k5 - Sorts by the 5th column
-n - Sorts numerically
-n 3 - Shows last 3 lines
Key Concept

If you remember nothing else from this pattern, remember: pipes let you connect simple commands so their outputs flow directly into the next command, creating powerful workflows without extra files.

Common Mistakes
Using a space instead of a pipe symbol between commands
Commands run separately and do not pass output to each other, so the workflow breaks.
Use the pipe symbol '|' exactly with spaces around it to chain commands.
Trying to pipe commands that do not read from standard input
The second command ignores the input, so the pipe has no effect.
Make sure the receiving command can accept input from the pipe, or use other methods like command substitution.
Forgetting to quote search patterns with spaces in grep after a pipe
The shell splits the pattern incorrectly, causing grep to fail or behave unexpectedly.
Always quote patterns with spaces, e.g., grep "error message".
Summary
Pipes connect commands so the output of one becomes the input of the next.
This lets you build workflows by combining simple commands without temporary files.
Use the pipe symbol '|' carefully to chain commands and create powerful one-liners.