📘 Command Grouping Using cmd1 | cmd2 (Pipe Operator)
1. What You Will Learn
- Understand the concept of piping between commands.
- Learn how
cmd1 | cmd2transfers standard output (stdout) from one command as standard input (stdin) to another. - Explore real-world use in WordPress VPS tasks like searching logs, filtering processes, or analyzing WP-CLI outputs.
- Identify differences between pipes and redirection (
>,<). - Combine pipes with advanced text utilities like
grep,awk, andsortfor automation. - Recognize common mistakes and performance caveats when chaining multiple pipes.
2. 5W + 1H Framework
| Element | Description |
| What | is called the pipe operator. It’s one of the most powerful features in shell scripting — it allows chaining commands so that the output of the first command (cmd1) becomes the input of the second (cmd2). |
| Why | It allows processing of large or filtered data streams without creating temporary files, making scripts more efficient. |
| Who | Useful for system administrators, DevOps engineers, and WordPress VPS maintainers who manage logs, monitor performance, and automate WP-CLI tasks. |
| Where | Commonly used in Linux shell pipelines, cron jobs, and real-time monitoring scripts. |
| When | Used when you need to filter, sort, or extract data from another command’s output. |
| How | By joining commands with ` |
3. Core Syntax & Concept
The pipe operator (|) in Bash connects two or more commands so that the standard output (stdout) of the command on the left becomes the standard input (stdin) of the command on the right.
You can think of it as building a data pipeline that flows through a sequence of filters.
3.1 Basic Syntax
cmd1 | cmd2
cmd1produces output.cmd2reads that output as its own input. The pipe does not create any temporary files; everything is handled in memory, which makes it extremely fast and efficient.
Example 1 – Filtering Text
cat /var/log/syslog | grep "php-fpm"
Explanation:
cat prints the contents of /var/log/syslog.
The output stream is sent to grep, which filters only lines that contain "php-fpm".
Expected Output Example
Oct 10 14:12:23 server php-fpm[1548]: pool wordpress
Oct 10 14:13:01 server php-fpm[1548]: request took 1.23s
Use Case: Monitoring PHP-FPM log events without manually opening large log files.
Example 2 – Chaining Multiple Commands
ps aux | grep php | sort -rk 3 | head -5
Explanation:
ps auxlists all running processes.grep phpfilters only PHP-related processes.sort -rk 3sorts by the 3rd column (CPU usage) in reverse order.head -5displays the top 5 entries. Expected Output
root 1492 12.3 1.2 517284 24684 ? S 14:10 0:03 php-fpm: pool www
www-data 1520 10.5 0.9 505684 18512 ? S 14:10 0:02 php-fpm: pool www
...
Use Case: Quickly check which PHP processes are consuming the most CPU on a VPS running WordPress.
Example 3 – WordPress Automation via WP-CLI
wp plugin list --allow-root | grep active
Explanation:
wp plugin list prints all plugins with their status.
grep active filters only the lines showing active plugins.
Expected Output
akismet active
litespeed-cache active
woocommerce active
Use Case: Auditing active plugins across multiple WordPress sites during maintenance.
Example 4 – Sorting Disk Usage
du -h /var/www | sort -hr | head -10
Explanation:
du -h displays human-readable folder sizes.
sort -hr sorts them from largest to smallest.
head -10 shows only the top 10 entries.
Expected Output
3.4G /var/www/site1
2.1G /var/www/site2
...
Use Case: Identify the heaviest websites or upload directories on the server.
Example 5 – Extracting Specific Columns
ls -lh /home | awk '{print $9, $5}'
Explanation:
ls -lh lists files with human-readable sizes.
awk prints only the file name ($9) and size ($5).
Expected Output
wordpress 1.2G
metaxenith 950M
devwp 310M
Use Case: Generate a concise overview of directory sizes for capacity planning.
Example 6 — Filter Output
ls -l | grep ".txt"
Explanation:
ls -llists files in long format.- The output is piped (
|) intogrep ".txt". grepfilters only lines containing.txt. Expected Output:
-rw-r--r-- 1 root root 824 Oct 12 notes.txt
-rw-r--r-- 1 root root 192 Oct 12 todo.txt
Example 7 — Count Processed Lines
ps aux | wc -l
Explanation:
ps auxlists all running processes.wc -lcounts the number of lines.- The result shows how many processes are running. Expected Output:
127
Example 8 — Combining Tools (WordPress Server Context)
wp plugin list --allow-root | grep "active"
Explanation:
wp plugin listlists all plugins in the current WordPress site.grep "active"filters only the active ones. Expected Output:
akismet active
litespeed-cache active
perfmatters active
4. Practical Use Cases
| Use Case | Command Example | Expected Output |
| Filter PHP errors from logs | `cat /var/log/syslog | grep php` |
| Find heavy processes | `ps aux | sort -rk 3 |
| Check active plugins in WordPress | `wp plugin list --allow-root | grep active` |
| Count active PHP-FPM workers | `ps aux | grep php-fpm |
| Analyze web access frequency | `cat access.log | awk '{print $1}' |
| Find largest uploads | `du -h /home/user/uploads | sort -hr |
| Monitor WordPress cron jobs | `wp cron event list --allow-root | grep publish` |
5. Benefits
| Category | Description |
| Efficiency | No temporary files – uses data streams directly. |
| Composability | Small commands can be combined into powerful pipelines. |
| Automation | Perfect for cron jobs and WordPress maintenance scripts. |
| Readability | Easier to follow logical flow than nested commands. |
6. Implementation Steps
- Identify the main command that produces data (e.g.,
wp,ps,cat). - Decide which filter command should process the output (
grep,awk,sort, etc.). - Connect them using the pipe (
|) operator. - Test the pipeline interactively in the terminal.
- Embed successful pipelines into automation scripts or aliases.
7. Best Practices
| Practice | Description |
| Use simple, single-purpose commands | Easier debugging and maintenance. |
| Keep pipes short (≤ 3 stages) | Avoid performance overhead. |
Combine with redirection (> logfile.txt) for persistent logs | Useful for auditing. |
| Use quotes for complex patterns | Prevents shell misinterpretation. |
| Validate output after each pipe | Detect broken commands in sequence. |
8. Static vs Dynamic Framing (WordPress Context)
| Type | Example | Behavior |
| Static | `cat /var/log/syslog | grep "php"` |
| Dynamic | `wp plugin list | grep active` |
9. Go-Live Checklist
- Verify each command individually before piping.
- Escape special characters when using regex filters.
- Log pipeline output when used in cron or long scripts.
- Use
teewhen you need both screen and file output. Example:
wp plugin list --allow-root | tee /root/plugin-status.log | grep active
10. Troubleshooting Matrix
| Symptom | Possible Cause | Solution |
| No output shown | Upstream command produces no stdout | Run cmd1 alone to test output. |
| “Broken pipe” error | Receiving command exits early | Validate data format and command compatibility. |
| Unexpected extra lines | Command includes stderr | Use 2>/dev/null to suppress errors. |
| Permission denied | wp or log file requires root access | Add --allow-root or adjust permission. |
11. Quick Lab
Task:
Filter all active plugins on all WordPress sites hosted under /home.
for site in /home/*/*/public_html; do
echo "Checking: $site"
wp plugin list --path="$site" --allow-root | grep active
echo "-----------------------------------"
done
Expected Output:
Checking: /home/site1/public_html
akismet active
litespeed-cache active
-----------------------------------
Checking: /home/site2/public_html
woocommerce active
-----------------------------------
12. Cheat Sheet
| Operator | Description | Example |
| ` | ` | Pipe output of one command as input to another |
| ` | tee file` | Save and display output simultaneously |
| ` | wc -l` | Count number of lines |
| ` | sort -r` | Reverse-sort output |
| ` | uniq -c` | Count unique occurrences |
13. Mini-Quiz
| # | Question | Answer |
| 1 | What does the pipe (` | `) operator do? |
| 2 | How is a pipe different from > redirection? | Pipe transfers output between commands, not to a file. |
| 3 | Can you use multiple pipes in a single line? | Yes, e.g., `cmd1 |
| 4 | How to view both screen output and save to file simultaneously? | Use tee, e.g., `cmd |
| 5 | What happens if the first command has no stdout? | The next command receives no input, so no output. |