Linux command list

uniq command: collapse repeated lines and count them

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying uniq. Nothing here can touch your real files.
user@webterm:~/project$
 

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

Syntax

uniq [options] file

It collapses adjacent identical lines into one.

Try it first

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.

Scattered duplicates survive

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.

Count, then order by the count

$ 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

Only the repeats, only the one-offs

-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.

When you actually reach for it

uniq is what you type to find out how many times the same thing happened.

SituationWhat to type
Tally, most frequent firstsort access.log | uniq -c | sort -nr
Find duplicate entriessort emails.txt | uniq -d
Find the one-offssort emails.txt | uniq -u
Count a repeated error in placeuniq -c errors.log
Just remove duplicatessort -u emails.txt

Things that trip people up

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 directory

Practise it hands-on

webterm.appthis site

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

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

Related commands

sort / wc / cut / grep

Frequently asked questions

The duplicates are still there.
uniq only compares each line with the one before it. Sort the input first so identical lines end up next to each other.
How is this different from sort -u?
If you only want the duplicates gone, `sort -u` is enough. uniq earns its place through `-c` (counts) and `-d` (duplicates only).
How do I get the most common first?
`sort | uniq -c | sort -nr`. Group, count, then order by that count.
What is the difference between -d and -u?
`-d` prints only lines that appeared more than once; `-u` prints only lines that appeared exactly once. Use -d to find repeats and -u to find one-offs.
Why are the counts padded?
`-c` right-aligns them, which keeps the columns straight and lets you pipe the result into `sort -nr` unchanged.