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.
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 | head02What 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
!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 manualEvidence: -c prefixes the number of occurrences; documents that uniq only compares adjacent equal lines
- uniq(1P) — POSIX manual pageEvidence: POSIX specifies that input must be sorted for uniq to detect all repeated lines
Last verified: 2026-09-17 · Category: Files & Folders