Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 sort first, uniq second.
  • Fast and simple: No flags needed for the common cases.

uniq only checks adjacent lines — which is why sort is its partner in every real pipeline.


1. Remove Duplicate Lines

sort names.txt | uniq
  • sort groups identical lines together first; uniq then 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
  • -c prefixes 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 sort for the second sort.

3. Show Only Duplicates or Only Unique Lines

sort names.txt | uniq -d
  • -d prints only lines that appear more than once — the duplicates themselves.
sort names.txt | uniq -u
  • -u prints 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 2 ignores 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, uniq compares 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

  • uniq on unsorted input barely works. It only compares adjacent lines. Always sort first unless the file is already grouped.
  • uniq -c output needs sort -rn to be useful. Counts in alphabetical order are hard to read; sort them numerically.
  • uniq removes only identical lines. Trailing spaces, different capitalization, or tab-vs-space differences make “same” lines look different. sort -f or sed can normalize first.
  • uniq -d prints each duplicated line once, not every occurrence — use -c when you want to see the counts.
  • Large files with uniq -c are fine, but uniq on truly unsorted huge files won’t catch everything — sort is the safe path.

PageRelationship
sortRequired first step for uniq to see all duplicates.
wcCounts the final result of a uniq pipeline.
grepFinds the lines uniq then counts.
diffCompares whole files instead of line repetition.

uniq Cheatsheet

CommandWhat It Does
sort f | uniqRemove duplicate lines.
sort f | uniq -cPrefix each line with its count.
sort f | uniq -dShow only duplicated lines.
sort f | uniq -uShow only lines appearing once.
uniq f1 f2Mark lines common to two files.
sort f | uniq -f 2Ignore the first 2 fields when comparing.
sort f | uniq -c | sort -rnCount occurrences, most first.
sort f | uniq -c | headTop repeated lines.