π§ͺ Linux/Unix: Pipes & Filters β |, tee, xargs, sort, uniq Explained
π§² Introduction β Why Learn Pipes & Filters in Linux/Unix?
In Linux/Unix, pipes and filters allow you to build powerful command chains to transform and analyze text streams. Using tools like |, tee, xargs, sort, and uniq, you can filter, redirect, and process data efficiently in real-time, all from the terminal.
π― In this guide, youβll learn:
- How to chain commands using pipes
- Use
teeto split output to screen and file - Pass arguments dynamically with
xargs - Filter data with
sortanduniq - Real-world usage for scripting and automation
π | β Pipe Operator
β Syntax:
command1 | command2
π Description:
Takes the stdout of the first command and passes it as stdin to the next.
π§ͺ Example:
ls -l | grep ".sh"
β
Lists only .sh files from the current directory.
π§ Pipes are the foundation of UNIX-style command chaining.
π tee β Split Output to File and Terminal
β Syntax:
command | tee filename
π Description:
- Displays command output on screen and saves it to a file.
- Useful for logging and monitoring simultaneously.
π§ͺ Example:
df -h | tee disk_report.txt
β
Output is shown and written to disk_report.txt.
π§ Append using:
command | tee -a logfile.txt
π xargs β Build Commands from Input
β Syntax:
command | xargs command2
π Description:
Reads items from stdin and executes the specified command with them as arguments.
π§ͺ Examples:
echo "file1.txt file2.txt" | xargs rm
β Deletes both files.
Combine with find:
find . -name "*.log" | xargs grep "error"
β
Searches “error” inside all .log files.
π’ sort β Sort Input Text
β Syntax:
sort [options] filename
π Description:
Sorts lines in a file or stream alphabetically or numerically.
π§ͺ Examples:
sort names.txt
sort -r names.txt # Reverse sort
sort -n numbers.txt # Numeric sort
β Use with pipe:
cat file.txt | sort | uniq
π uniq β Filter Repeated Lines
β Syntax:
uniq [options] filename
π Description:
Removes consecutive duplicate lines. Often used with sort.
π§ͺ Examples:
uniq list.txt
sort list.txt | uniq # Remove all duplicates
uniq -c sorted.txt # Show counts of duplicates
β
uniq only works on adjacent linesβuse sort first to group duplicates.
βοΈ Combining Pipes and Filters β Real-World Example
π Example: Find most frequent IPs from a log file
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head
π Breakdown:
awk '{print $1}'β extract IPssortβ group identical IPsuniq -cβ count duplicatessort -nrβ sort by frequencyheadβ show top 10
π Summary β Recap & Next Steps
Pipes and filters let you build mini data processing pipelines in your terminal. Whether parsing logs, removing duplicates, or passing file lists dynamically, tools like |, tee, xargs, sort, and uniq make Linux command-line work fast, scriptable, and powerful.
π Key Takeaways:
|connects output of one command to the input of another.teedisplays and writes output to a file.xargstransforms input into arguments for other commands.sortanduniqclean and deduplicate data.- Combine them for real-time stream processing.
β FAQs
β Whatβs the difference between | and xargs?
β
| connects output between commands; xargs builds command-line arguments from input.
β How do I log and view output at the same time?
β
Use:
command | tee output.log
β Why does uniq not remove all duplicates?
β
It only removes consecutive duplicates. Sort the input first:
sort file.txt | uniq
β Can I delete files listed in a text file using xargs?
β
Yes:
cat files.txt | xargs rm
β How do I append tee output to an existing file?
β
Use:
command | tee -a logfile.txt
Share Now :
