πŸ“‘Linux/Unix: File Viewing, Redirection & Filters
Estimated reading: 3 minutes 38 views

πŸ§ͺ 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 tee to split output to screen and file
  • Pass arguments dynamically with xargs
  • Filter data with sort and uniq
  • 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 IPs
  • sort β†’ group identical IPs
  • uniq -c β†’ count duplicates
  • sort -nr β†’ sort by frequency
  • head β†’ 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.
  • tee displays and writes output to a file.
  • xargs transforms input into arguments for other commands.
  • sort and uniq clean 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: Pipes & Filters (|, tee, xargs, sort, uniq)

Or Copy Link

CONTENTS
Scroll to Top