0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use uniq Command in Linux: Syntax and Examples

The uniq command in Linux filters out or reports repeated lines in a sorted file or input. Use uniq with options like -c to count duplicates or -d to show only repeated lines.
📐

Syntax

The basic syntax of the uniq command is:

  • uniq [options] [input_file]

Here:

  • input_file is the file to read (or standard input if omitted).
  • options modify behavior, like counting or showing duplicates.
bash
uniq [options] [input_file]
💻

Example

This example shows how to use uniq to filter repeated lines from a sorted list and count duplicates.

bash
echo -e "apple\napple\nbanana\nbanana\nbanana\ncherry" | sort | uniq

echo -e "apple\napple\nbanana\nbanana\nbanana\ncherry" | sort | uniq -c
Output
apple banana cherry 2 apple 3 banana 1 cherry
⚠️

Common Pitfalls

A common mistake is using uniq on unsorted input, which only removes adjacent duplicates. Always sort input first if you want to remove all duplicates.

Also, uniq only filters adjacent lines, so lines must be grouped.

bash
echo -e "apple\nbanana\napple" | uniq

# Wrong: does not remove non-adjacent duplicates

echo -e "apple\nbanana\napple" | sort | uniq

# Right: sorts first to group duplicates
Output
apple banana apple apple banana apple
📊

Quick Reference

OptionDescription
-cPrefix lines by the number of occurrences
-dShow only duplicate lines
-uShow only unique lines
-iIgnore case when comparing
-f NSkip first N fields
-s NSkip first N characters

Key Takeaways

Use uniq to filter or count repeated adjacent lines in sorted input.
Always sort input before using uniq to remove all duplicates.
Use -c to count occurrences and -d to show only duplicates.
Remember uniq works only on adjacent lines, not the whole file unless sorted.
Combine uniq with other commands like sort for best results.