Master Linux Pipelines and Redirection: Practical Commands and Real-World Examples
Learn how to boost your Linux command-line efficiency by mastering pipelines and redirection, with clear examples covering basic usage, multi-stage chaining, output redirection, tee, log monitoring, batch processing, awk, xargs, custom scripts, and process substitution for advanced data handling.
Introduction
In a Linux command‑line environment, pipelines (|) and redirection are powerful tools that can dramatically improve the efficiency and flexibility of command execution. This guide explores advanced usage of these features with detailed code examples.
Basic Usage of Pipelines
Simple Command Combination
Connect two or more commands so that the output of one becomes the input of the next.
# Example: view a log file and filter for "error"
cat /var/log/syslog | grep "error"Multi‑Stage Pipelines
Chain multiple commands to create more complex data‑processing flows.
# Example: list files and sort by size
ls -l | sort -k5Basic Redirection
Redirect Standard Output to a File
Save a command’s standard output for later review.
# Example: save directory listing to list.txt
ls > list.txtRedirect Error Output
Capture error messages to a file for debugging.
# Example: save error messages to error.log
command_that_might_fail 2> error.logCombining Pipelines and Redirection
Filter with grep and Save Results
Use a pipeline to filter data and then redirect the filtered output.
# Example: filter log entries and save to result.txt
cat /var/log/syslog | grep "pattern" > result.txtRedirect Output of Multiple Commands
Store the combined result of several commands in a file.
# Example: list files, filter with grep, and save
ls -l | grep "keyword" > output.txtUsing tee for Simultaneous Output
The tee command writes to both the screen and a file.
# Example: display and save directory listing
ls -l | tee output.txtReal‑World Scenarios
Live Log Monitoring
Combine tail with a pipeline to filter log entries in real time.
# Example: monitor syslog for "error" entries
tail -f /var/log/syslog | grep "error"Batch File Processing
Use find to locate files and pipe the list to other commands.
# Example: copy all .txt files to a backup directory
find . -name "*.txt" | xargs -I {} cp {} /backupUsing awk for Text Manipulation
awkcan extract and reformat columns, especially when combined with pipelines.
# Example: extract columns 2 and 3 from data.txt
cat data.txt | awk '{print $2,$3}' > extracted_data.txtUsing xargs for Bulk Operations
xargsreads items from standard input and passes them as arguments to another command.
# Example: delete all .txt files found by find
find . -name "*.txt" | xargs rmCombining Pipelines, Redirection, awk , and xargs
Count Keyword Occurrences in Logs
Chain multiple tools to count how often a keyword appears and store the result.
# Example: count "error" occurrences and save
cat /var/log/syslog | grep "error" | awk '{print $4}' | sort | uniq -c > error_count.txtCustom Script Integration
Processing Data with a User Script
Feed pipeline data into a custom script for more complex logic.
# Example: process log file with a custom script
cat /var/log/syslog | my_custom_script.sh > processed_data.txtProcess Substitution
Use process substitution to treat command output as a temporary file.
# Example: compare two generated files without creating them
diff <(cat file1.txt) <(cat file2.txt)Conclusion
By mastering pipelines, redirection, and related utilities such as tee, awk, xargs, custom scripts, and process substitution, you can handle complex data‑processing tasks efficiently in the Linux shell, greatly enhancing productivity in everyday operations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
