Skip to main content

uniq

The uniq command filters out adjacent, repeated lines in a file. It is often used in combination with the sort command since uniq only detects duplicates that are next to each other.

Basic Syntax

uniq [OPTION]... [INPUT [OUTPUT]]

Common Options

OptionDescription
-c, --countPrefix lines by the number of occurrences.
-d, --repeatedOnly print duplicate lines, one for each group.
-u, --uniqueOnly print unique lines (lines that do not have duplicates).
-i, --ignore-caseIgnore differences in case when comparing.
-f, --skip-fields=NAvoid comparing the first N fields.

Real-world Examples

  1. Remove adjacent duplicate lines:

    uniq items.txt
  2. Count occurrences of each line:

    sort items.txt | uniq -c
  3. Show only duplicate lines:

    sort items.txt | uniq -d
  4. Show only unique lines:

    sort items.txt | uniq -u