The uniq Command
uniq removes adjacent duplicate lines. That's the key word: adjacent.
Basic Usage
Notice: the third "apple" wasn't removed because it's not adjacent to the first two.
Sort First!
uniq only removes adjacent duplicates. To remove ALL duplicates, sort first:
sort file.txt | uniq
Or just use sort -u.
The Sort + Uniq Pattern
This is the standard pattern: sort | uniq.
Count Occurrences
-c counts how many times each line appears.
Most Common Pattern
Find the most common items:
sort data.txt | uniq -c | sort -rn | head
This is incredibly useful for log analysis.
Show Only Duplicates
-d shows only lines that appear more than once.
Show Only Unique Lines
-u shows only lines that appear exactly once.
Case-Insensitive
-i ignores case differences.
Real-World Examples
Top 10 IP Addresses
Find Duplicate Lines
Count Unique Errors
Files Modified by User
Uniq vs Sort -u
These are almost the same:
sort file.txt | uniq # Classic way
sort -u file.txt # Shorter
Use sort -u when you just want unique lines.
Use sort | uniq -c when you need counts.
Why doesn't `uniq file.txt` always remove all duplicates?
Quick Reference
| Flag | Effect |
|---|---|
-c | Count occurrences |
-d | Only show duplicates |
-u | Only show unique (non-duplicates) |
-i | Case-insensitive |
Key Takeaways
uniqremoves adjacent duplicates only- Always sort first:
sort | uniq -ccounts occurrences - incredibly usefulsort file | uniq -c | sort -rn | head= top N most common-dfinds duplicates,-ufinds unique linessort -uis shorthand forsort | uniq
Next: counting with wc.