The uniq command filters adjacent matching lines from input, effectively removing or reporting duplicates. It's a fundamental tool for data deduplication and analysis. Important: uniq only compares adjacent lines, so input typically needs to be sorted first (using sort) to catch all duplicates.
Key Features:
Deduplication Remove duplicate lines
Count Mode Count occurrence frequency
Filter Unique/Duplicate Show only unique or only duplicated lines
Field Selection Compare specific fields only
⚠️ Critical: uniq only detects adjacent duplicate lines. Always sort your data first: sort file.txt | uniq
Command Syntax
uniq [OPTIONS] [INPUT [OUTPUT]]
Essential Options
Option
Description
Example
-c
Count occurrences (prefix lines)
sort file.txt | uniq -c
-d
Only show duplicate lines
sort file.txt | uniq -d
-u
Only show unique lines (non-duplicated)
sort file.txt | uniq -u
-i
Ignore case
sort -f file.txt | uniq -i
-f N
Skip first N fields
sort file.txt | uniq -f 2
-s N
Skip first N characters
sort file.txt | uniq -s 5
-w N
Compare only first N characters
sort file.txt | uniq -w 10
-D
Show all duplicate lines (not just one)
sort file.txt | uniq -D
Detailed Examples
Example 1: Basic Duplicate Removal
cat << EOF | uniq
apple
apple
banana
cherry
cherry
cherry
date
EOF
apple
banana
cherry
date
Basic uniq removes adjacent duplicate lines:
Two consecutive "apple" lines become one
Three consecutive "cherry" lines become one
Single "banana" and "date" lines unchanged
Only works on adjacent duplicates!
⚠️ Adjacent Only:
# This won't work as expected!
echo -e "apple\\nbanana\\napple" | uniq
# Output: apple, banana, apple (both apples shown!)
# Correct way:
echo -e "apple\\nbanana\\napple" | sort | uniq
# Output: apple, banana
Example 2: Count Occurrences
cat << EOF | sort | uniq -c
apple
banana
apple
cherry
banana
apple
cherry
cherry
EOF
3 apple
2 banana
3 cherry
The -c option counts occurrences and prefixes each line:
Shows frequency of each unique line
Count is right-aligned with leading spaces
Essential for frequency analysis
Combine with sort -rn to find most common items
💡 Power Pattern: Find most frequent items:
sort file.txt | uniq -c | sort -rn | head -10
Example 3: Show Only Duplicates
cat << EOF | sort | uniq -d
apple
banana
apple
cherry
date
apple
EOF
apple
The -d option shows only lines that appear more than once:
Shows each duplicate line exactly once
Perfect for finding what data is duplicated
Useful for data quality checks
banana, cherry, date don't appear (only once each)
Example 4: Show Only Unique Lines
cat << EOF | sort | uniq -u
apple
banana
apple
cherry
date
EOF
banana
cherry
date
The -u option shows only lines that appear exactly once:
Opposite of -d
Shows truly unique items (non-duplicated)
apple doesn't appear (it's duplicated)
Useful for filtering out repeated entries
Example 5: Case-Insensitive Comparison
cat << EOF | sort -f | uniq -i
Apple
APPLE
apple
Banana
banana
EOF
# Find items in file1 but not in file2
sort file1.txt file2.txt file2.txt | uniq -u
# Find common items between files
sort file1.txt file2.txt | uniq -d
Performance Considerations
⚡ Performance Tips:
Sort First: Always sort before uniq for correct results
Use sort -u: For simple deduplication, sort -u is faster than sort | uniq
Large Files: uniq is very memory efficient (processes line by line)
LC_ALL=C: Set for faster sorting: LC_ALL=C sort file.txt | uniq
📊 Benchmark:
# Fastest for simple deduplication
time sort -u huge_file.txt > output.txt
# Slower but necessary for counting
time sort huge_file.txt | uniq -c > output.txt
Common Pitfalls and Solutions
Pitfall 1: Forgetting to Sort
⚠️ Wrong:
# This misses non-adjacent duplicates!
uniq file.txt
# Example:
echo -e "a\\nb\\na" | uniq # Output: a, b, a (WRONG!)
✅ Correct:
sort file.txt | uniq
# Example:
echo -e "a\\nb\\na" | sort | uniq # Output: a, b (correct)
Pitfall 2: Using uniq -c with Pipes
⚠️ Problem: Spaces in count output complicate parsing