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 :
