uniq Command Guide for Finding Duplicates
This guide helps you understand and use the uniq command, the standard way to report or remove repeated lines in sorted text.
What is uniq, and Why Use It?
The uniq command reads a stream and reports or filters repeated adjacent lines. It counts how many times each line occurs, removes duplicates, or shows only what appears more than once. Its name means “unique” — it makes repeated lines unique, or tells you about them.
Why Learn uniq?
- Counting things: “Which IPs hit our server most?” is
sort | uniq -c. - Dedupe lists: Remove duplicate usernames, domains, or file entries from any list.
- It’s half of the classic pipeline: Almost every “top N” analysis needs
sortfirst,uniqsecond. - Fast and simple: No flags needed for the common cases.
uniqonly checks adjacent lines — which is whysortis its partner in every real pipeline.
1. Remove Duplicate Lines
sort names.txt | uniq
sortgroups identical lines together first;uniqthen keeps one of each.
uniq alone only removes adjacent duplicates, so unsorted files often keep duplicates. Test it yourself:
printf "a\nb\na\n" | uniq
That prints a b a — the two as are not adjacent, so both survive. This is the single most common uniq confusion.
2. Count Occurrences
sort data.txt | uniq -c
-cprefixes each line with its count.
A practical example — count how many times each HTTP status appears in a log:
sort access.log | uniq -c | sort -rn | head
- That counts, sorts by count descending, and shows the top 10. See
sortfor the second sort.
3. Show Only Duplicates or Only Unique Lines
sort names.txt | uniq -d
-dprints only lines that appear more than once — the duplicates themselves.
sort names.txt | uniq -u
-uprints only lines that appear exactly once.
Together they answer “which entries are repeated?” and “which entries are one-offs?” — useful for auditing config files and word lists.
4. Check Adjacent Duplicates Without Sorting
uniq -d already-sorted.txt
If the input is already sorted (or has single occurrences), uniq works directly on the file — no sort needed. This is why uniq reads a stream: it makes one pass, never buffering the whole file.
Skip the first N fields when comparing (for log lines with timestamps):
sort log.txt | uniq -f 2 -c
-f 2ignores the first 2 fields when deciding what’s “the same” — so log lines differing only in timestamp still count together.
5. Compare Two Files
uniq file1.txt file2.txt
- With two inputs,
uniqcompares them and marks lines that appear in both with a|prefix.
This is a quick manual alternative to diff when you only care about which entries overlap.
6. Combine with Pipes
The full classic, counting repeated errors:
grep "ERROR" app.log | sort | uniq -c | sort -rn | head -n 5
- Find errors with
grep, sort them, count each unique message, sort by count, show the top 5.
Count unique users from a log’s fourth column:
awk '{print $4}' access.log | sort | uniq | wc -l
That prints one number: the count of distinct values. wc does the final count.
Common Pitfalls
uniqon unsorted input barely works. It only compares adjacent lines. Alwayssortfirst unless the file is already grouped.uniq -coutput needssort -rnto be useful. Counts in alphabetical order are hard to read; sort them numerically.uniqremoves only identical lines. Trailing spaces, different capitalization, or tab-vs-space differences make “same” lines look different.sort -forsedcan normalize first.uniq -dprints each duplicated line once, not every occurrence — use-cwhen you want to see the counts.- Large files with
uniq -care fine, butuniqon truly unsorted huge files won’t catch everything — sort is the safe path.
Related Pages
| Page | Relationship |
|---|---|
sort | Required first step for uniq to see all duplicates. |
wc | Counts the final result of a uniq pipeline. |
grep | Finds the lines uniq then counts. |
diff | Compares whole files instead of line repetition. |
uniq Cheatsheet
| Command | What It Does |
|---|---|
sort f | uniq | Remove duplicate lines. |
sort f | uniq -c | Prefix each line with its count. |
sort f | uniq -d | Show only duplicated lines. |
sort f | uniq -u | Show only lines appearing once. |
uniq f1 f2 | Mark lines common to two files. |
sort f | uniq -f 2 | Ignore the first 2 fields when comparing. |
sort f | uniq -c | sort -rn | Count occurrences, most first. |
sort f | uniq -c | head | Top repeated lines. |