shell · difficulty ◆◆
uniq — report or omit repeated lines
Find the unique lines in a file — and count how many times each one shows up.
$ uniqWhat it does
uniq reads lines of input and removes adjacent duplicates, printing each unique line once. Because it only compares neighboring lines, it is almost always paired with sort first — sort groups identical lines together, then uniq collapses them. Add -c and it prefixes each line with a count of how many times it appeared.
Why it matters
uniq is the standard way to answer 'how many times does each thing occur?' in a text stream. Combined with sort, cut, and awk, it turns raw logs and lists into clean summaries: top visitors, most common error codes, most frequent words. It is a small command, but it is the difference between staring at a wall of log lines and seeing the answer.
Example
$ sort access.log | cut -d' ' -f1 | uniq -c | sort -rn | head -5 482 203.0.113.5
391 198.51.100.22
210 192.0.2.77
88 203.0.113.9
12 198.51.100.1The classic pipeline: sort, extract the IP field, count duplicates with -c, then sort by count descending to find the top visitors.
$ printf 'apple\nbanana\napple\napple\ncherry\n' | sort | uniqapple
banana
cherrysort groups the identical lines together, then uniq prints each distinct value once. Without sort, uniq would miss the separated 'apple' lines.
Common flags
- -c
- prefix each line with the number of times it occurred
- -d
- print only duplicated lines (one copy of each)
- -u
- print only lines that appear exactly once
- -i
- ignore case when comparing lines
- -f N
- skip the first N fields before comparing
- -s N
- skip the first N characters before comparing
- -w N
- compare only the first N characters of each line
History
Origin
uniq has been part of Unix since the early 1970s, appearing in the first editions of the system at Bell Labs. It was designed as a companion to sort — the two were meant to be used together, and their pairing is one of the earliest examples of the Unix philosophy of small tools that compose through pipes.
Fun fact
The 'adjacent-only' behavior is a deliberate design choice, not a limitation. By requiring sorted input, uniq stays stateless and memory-light — it only ever needs to remember one line at a time, which is why it can process files of any size without loading them into memory.