uniq
Report or filter out repeated adjacent lines
By CMD Script Team · 4 min read · Last updated
uniq [OPTIONS] [INPUT [OUTPUT]]Options
| Flag | Description |
|---|---|
-c | Prefix each output line with the count of matching adjacent lines |
-d | Print only lines that are duplicated (appear more than once adjacently) |
-u | Print only lines that are unique (appear exactly once adjacently) |
-i | Ignore case when comparing lines for duplication |
-f | Skip the first N fields when comparing lines |
-s | Skip the first N characters when comparing lines |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
uniq filters adjacent matching lines from input, either collapsing them down to one
occurrence, counting them, or showing only the duplicated or only the unique ones. The
critical detail is "adjacent" — uniq compares each line only to the one immediately
before it, so it does not detect duplicates scattered throughout an unsorted file. That's
why uniq is almost always used right after sort.
Beginner examples
uniq file.txt— collapse adjacent duplicate lines (file must already be sorted for full effect)sort file.txt | uniq— the standard way to get a fully deduplicated listsort file.txt | uniq -c— count how many times each line occurssort file.txt | uniq -d— show only lines that repeat
sort visitors.txt | uniq -c
Advanced examples
- Rank lines by frequency, most common first:
sort access.log | uniq -c | sort -rn | head - Find usernames that appear only once in a login log (rare/first-time users):
sort logins.txt | uniq -u - Deduplicate case-insensitively for messy user-entered data:
sort emails.txt | uniq -i - Compare lines ignoring the first field (e.g. a timestamp column):
sort log.txt | uniq -f 1 - Count unique IP addresses hitting a server, sorted by frequency:
awk '{print $1}' access.log | sort | uniq -c | sort -rn
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -20
Common mistakes
- Running
uniqon an unsorted file and assuming all duplicates were removed — only adjacent duplicates are collapsed, so scattered repeats slip through untouched. - Forgetting
-cwhen you actually need frequency counts, then trying to reconstruct counts manually from plainuniqoutput. - Confusing
-d(show only duplicated lines) with-u(show only lines with no duplicates) — they're opposites, not variations of the same filter. - Piping
uniqbeforesortinstead of after, e.g.uniq file.txt | sort, which sorts already-incorrect output instead of fixing the underlying adjacency problem.
Tips
- Always sort first:
sort file | uniqis the standard idiom;uniqalone assumes pre-sorted input. - Use
uniq -ccombined with a secondarysort -rnto build a quick frequency-ranked report from raw text. sort -udoes an equivalent single-pass dedup without a count and without a separateuniqstep, when you don't need counts.
Best practices
- Treat "sort then uniq" as one atomic operation in scripts and pipelines; never rely on
uniqalone for deduplication unless you've explicitly guaranteed the input is sorted upstream. - Use
uniq -cfor frequency analysis rather than writing a manual counting loop — it's faster and less error-prone. - When comparing lines that share a variable prefix (like a timestamp), use
-f/-sto skip the fields/characters that shouldn't factor into the comparison, instead of pre-processing withcutfirst.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Finding the most frequent IP addresses hitting a web server from access logs:
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head. - Deduplicating a mailing list before a send:
sort -u subscribers.txt > subscribers_clean.txt. - Auditing a file of error codes to see which ones are truly one-off versus recurring:
sort errors.txt | uniq -c | sort -n.
Common interview questions
- Why does
uniqsometimes fail to remove duplicates?uniqonly compares adjacent lines; if the input isn't sorted, duplicate lines that aren't next to each other won't be collapsed. - How do you count occurrences of each unique line in a file?
sort file | uniq -c— sort groups identical lines together, thenuniq -cprefixes each with its count. - What's the difference between
uniq -danduniq -u?-dprints only lines that have duplicates (appeared more than once);-uprints only lines that are truly unique (appeared exactly once).
Frequently Asked Questions
Why doesn't uniq remove all duplicates from my file?
uniq only collapses ADJACENT duplicate lines, not duplicates anywhere in the file. If the file isn't sorted first, identical lines that are far apart won't be detected. The standard pattern is sort file.txt | uniq.
How do I count how many times each line appears?
Pipe sorted input into uniq -c, e.g. sort file.txt | uniq -c, which prefixes each unique line with its occurrence count.
How do I find only the lines that appear more than once?
Use sort file.txt | uniq -d to print only lines with duplicates, or sort file.txt | uniq -c | awk '$1 > 1' for more control over the threshold.
How do I find lines that are completely unique (never repeated)?
Use sort file.txt | uniq -u, which prints only lines that appear exactly once after sorting.
Cheat sheet
Download a quick-reference cheat sheet for uniq.