sort | uniq -c: count duplicate lines the right way

Find and rank repeated lines in logs on Linux with uniq -c, remembering that input must be sorted first.

LinuxuniqduplicatessortLow risk
Run sort FILE | uniq -c to prefix each unique line with its occurrence count, then sort -rn to rank them.

01Command

Command
$ sort access.log | uniq -c | sort -rn | head

02What it does

uniq's forgotten prerequisite is that it only compares adjacent lines — without sort in front of it, duplicates scattered through a file go undetected. With -c each output line gains a count column, -d shows only duplicated lines, -u only unique ones, and -i ignores case. The canonical 'most frequent lines' idiom is sort | uniq -c | sort -rn, which powers everything from top-IP lists to top-URL reports.

03Risk

RiskLow risk
!Warning: uniq never reports duplicates that aren't adjacent — always sort first or your counts will be wrong.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • uniq(1) — Arch Linux manual
    Arch Linux · technical · 2026-09-17
    Evidence: -c prefixes the number of occurrences; documents that uniq only compares adjacent equal lines
  • uniq(1P) — POSIX manual page
    man7.org · technical · 2026-09-17
    Evidence: POSIX specifies that input must be sorted for uniq to detect all repeated lines

Last verified: 2026-09-17 · Category: Files & Folders