Quick take: The uniq command filters out adjacent duplicate lines, so you almost always run sort first. Use uniq -c to count occurrences, -d to show only duplicates, and -u for lines that appear once.

Introduction

The uniq command removes or reports repeated lines. Its one quirk defines how you use it: uniq only collapses adjacent duplicates, so the input must be sorted first. Paired with sort, it becomes a powerful tool for counting and de-duplicating data.

This guide covers removing duplicates, counting occurrences, and isolating lines that are repeated or unique.

Syntax

The basic syntax of the uniq command is:

uniq [OPTIONS] [INPUT]

Common Options and Parameters

The most useful options and parameters for the uniq command:

OptionDescription
-cPrefix each line with its number of occurrences.
-dShow only lines that are duplicated.
-uShow only lines that appear exactly once.
-iIgnore case when comparing.
-f NSkip the first N fields when comparing.

Practical Examples

Real uniq commands you can run today:

# Remove adjacent duplicates (sort first!)
sort access.log | uniq
# Count how many times each line appears
sort names.txt | uniq -c
# Show only the duplicated lines
sort list.txt | uniq -d
# Show only lines that appear once
sort list.txt | uniq -u
# Top 10 most frequent values
sort log | uniq -c | sort -nr | head
# Count unique visitors by IP
awk '{print $1}' access.log | sort | uniq -c | sort -nr

Tips and Best Practices

  • Always sort before uniq — uniq only removes consecutive duplicates, so unsorted input gives wrong results.
  • uniq -c followed by sort -nr is the standard way to build a frequency-ranked list.
  • Use sort -u if you only need de-duplication without counts — it does both jobs in one command.

Final Thoughts

uniq is small but essential for analysing data — counting occurrences, finding duplicates, and isolating one-off lines. The golden rule is to sort first, because uniq only sees adjacent duplicates. Combined with sort and awk, it powers the log-analysis one-liners administrators rely on daily.

FAQ: uniq Command in Linux

Why do I need to sort before uniq?+

uniq only removes duplicate lines that are next to each other. Sorting groups identical lines together so uniq can collapse them; on unsorted input it misses duplicates that are far apart.

How do I count how many times each line appears?+

Use sort file | uniq -c. The -c flag prefixes each unique line with its occurrence count. Add | sort -nr to rank from most to least frequent.

How do I show only duplicate lines?+

Use uniq -d on sorted input: sort file | uniq -d prints only lines that appear more than once. Use -u for lines that appear exactly once.

What is the difference between sort -u and uniq?+

sort -u sorts and removes duplicates in one step but cannot count. uniq offers counting (-c), duplicate-only (-d), and unique-only (-u) views, but needs sorted input.

How do I find unique values in a column?+

Extract the column then sort and uniq it: awk '{print $1}' file | sort | uniq counts distinct values, and adding -c gives their frequencies.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support