← All commands
THE COMMAND REFERENCE

uniq

Report or omit repeated adjacent lines.

Last updated

macOS / Apple

Implementation-level reference; examples are not certified for every installed version.

Overview

uniq collapses runs of identical adjacent lines. It does not find duplicates that are not next to each other, so sort the input first when you want every duplicate treated alike.

Background

A uniq command appeared in Version 3 Unix, according to the FreeBSD and macOS manuals. It only collapses adjacent duplicate lines, which is why it usually follows sort. Today the name covers GNU uniq, the uniq that macOS ships, and the uniq applet in BusyBox. They agree on -c, -d, and -u, and differ on options such as -i and field skipping.

Examples

Collapse duplicates after sorting:

sort fruit.txt | uniq

Count how many times each line occurs:

sort fruit.txt | uniq -c

Show only lines that occur more than once:

sort fruit.txt | uniq -d

Show only lines that occur exactly once:

sort fruit.txt | uniq -u

Rank values by how often they occur:

sort fruit.txt | uniq -c | sort -rn

Without sorting, only adjacent repeats are removed, so this file is unchanged:

uniq fruit.txt

Options

-c
Prefix each line with the number of times it occurred in a row. The padding differs by
implementation.

-d
Print only one copy of each line that is repeated.

-u
Print only lines that are not repeated.

Common mistakes

uniq only compares neighbours, so unsorted input keeps non-adjacent duplicates. Use sort -u when you only need unique lines and do not need counts. A second file argument names an output file, which it overwrites, so pass input on standard input in scripts to be safe.

Portability

-c, -d, and -u are in the Common examples. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check). Options such as -i (ignore case) and field skipping differ between builds.

About macOS uniq

This is the uniq in /usr/bin on macOS. GNU long options such as --count are not available, so use the short spellings.

Apple notes

macOS uniq is the BSD implementation. GNU long options such as --count are not available; use the short spellings.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.