How to Remove Duplicate Lines in Linux (uniq)

uniq filters out adjacent duplicate lines from sorted input, leaving only unique lines.

Published October 2, 2026

uniq removes or counts adjacent duplicate lines, so the input must be sorted first. uniq -c shows how many times each line appears.

How it works

It compares only neighbouring lines, which is why sort | uniq is the standard pairing.

-d shows only duplicates, -u only unique lines.

Try it

sort names.txt | uniq -c
2 Ada
1 Bob

Practical examples

Deduplicate

sort file.txt | uniq

Count occurrences

sort file.txt | uniq -c | sort -rn

Only duplicated lines

sort file.txt | uniq -d

Dedupe without sorting

awk '!seen[$0]++' file.txt

Common mistakes

  • Running uniq without sorting.
  • Expecting case-insensitive match without -i.
  • Losing original order (use awk for that).

Key takeaways

  • uniq works on adjacent lines.
  • sort first.
  • -c counts.

FAQ

sort -u vs uniq?

sort -u sorts and dedupes in one step.

Ignore case?

uniq -i.

More Linux articles