uniq collapses adjacent identical lines. Learn why it is always paired with `sort`, how `-c` counts occurrences, and what `-d` and `-u` are for, by running the commands in a real terminal in your browser.
Updated: 2026-09-06
uniq [options] file
It collapses adjacent identical lines into one.
A log where the same error repeats.
$ cat logs/errors.log
ERROR connection refused
ERROR connection refused
ERROR connection refused
WARN slow query
ERROR timeout
ERROR timeout
Through uniq, each run of identical lines becomes a single line.
$ uniq logs/errors.log
ERROR connection refused
WARN slow query
ERROR timeout
-c says how many times each run repeated.
$ uniq -c logs/errors.log
3 ERROR connection refused
1 WARN slow query
2 ERROR timeout
The same error hit three times in a row.
This is where uniq surprises people.
$ cat emails.txt
ada@example.com
linus@example.com
ada@example.com
grace@example.com
linus@example.com
ada@example.com
$ uniq emails.txt
ada@example.com
linus@example.com
ada@example.com
grace@example.com
linus@example.com
ada@example.com
Not one line was removed, because the duplicates are not next to each other. Sort first.
$ sort emails.txt | uniq
ada@example.com
grace@example.com
linus@example.com
In practice, uniq almost always follows a sort.
$ sort emails.txt | uniq -c
3 ada@example.com
1 grace@example.com
2 linus@example.com
$ sort emails.txt | uniq -c | sort -nr
3 ada@example.com
2 linus@example.com
1 grace@example.com
Those three steps, group and count and order, are the standard way to read a log.
$ cut -d' ' -f1 logs/access.log | sort | uniq -c | sort -nr
3 200
2 404
1 500
-d keeps lines that appeared more than once.
$ sort emails.txt | uniq -d
ada@example.com
linus@example.com
-u keeps the lines that appeared exactly once.
$ sort emails.txt | uniq -u
grace@example.com
Use -d to hunt for duplicates and -u to find what only happened once.
uniq is what you type to find out how many times the same thing happened.
| Situation | What to type |
|---|---|
| Tally, most frequent first | sort access.log | uniq -c | sort -nr |
| Find duplicate entries | sort emails.txt | uniq -d |
| Find the one-offs | sort emails.txt | uniq -u |
| Count a repeated error in place | uniq -c errors.log |
| Just remove duplicates | sort -u emails.txt |
Without sort in front, nothing happens.
uniq compares neighbours only.
If duplicates refuse to disappear, that is the reason.
For removal alone, sort -u is enough.
$ sort -u emails.txt
ada@example.com
grace@example.com
linus@example.com
What uniq adds is -c and -d.
Directories are not accepted.
$ uniq logs
uniq: logs: Is a directory
A missing file says so.
$ uniq nothing.txt
uniq: nothing.txt: No such file or directorywebterm.appthis site
Terminal Fundamentals
Learn commonly used commands
learn.webterm.appa separate site

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.
See the course