Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

The uniq Command in Linux with Examples

Find, count and remove repeated lines in text files with uniq.

How-To Guides3 min read10 screenshots

uniq filters out repeated lines in a file or in piped input. It compares each line with the line before it, so repeated lines must be next to each other. For that reason you usually run sort first.

Syntax

uniq [OPTIONS] [INPUT_FILE [OUTPUT_FILE]]

Common options

  • -c, --count puts the number of times each line appears in front of it.
  • -d, --repeated shows only one copy of each repeated line.
  • -D shows all copies of the repeated lines.
  • -f N, --skip-fields=N ignores the first N fields when comparing.
  • -i, --ignore-case ignores letter case.
  • -s N, --skip-chars=N ignores the first N characters.
  • -w N, --check-chars=N compares only the first N characters.
  • -u, --unique shows only the lines that are not repeated.

Prepare a sample file

Create a file uh.txt with these lines:

sudo apt update
ls -l
ls -l
cd /var/log
cd /var/log
cd /var/log
sudo apt update

Remove consecutive duplicates

uniq uh.txt
Removing consecutive duplicate lines

Output:

sudo apt update
ls -l
cd /var/log
sudo apt update

The last sudo apt update stays because it is not next to the first one.

Show only the repeated lines

uniq -d uh.txt
Showing the repeated lines

Show every copy of the repeated lines

uniq -D uh.txt
Showing all instances of the repeated lines

Count the lines

uniq -c uh.txt
Counting the repeated lines

Sort first to count all duplicates, even those that are not next to each other:

sort uh.txt | uniq -c
Sorting before counting the lines

Change the output format

sort uh.txt | uniq -c | awk '{print $2 ": " $1}'
Customizing the output of uniq

To list the most frequent lines first:

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

Show only the lines that appear once

uniq -u uh.txt
Showing the non-repeated lines

Skip fields and characters

Skip the first 2 fields (words) when comparing:

sort uh.txt | uniq -f 2
Skipping the first N fields

Skip the first 3 characters:

sort uh.txt | uniq -s 3
Skipping the first N characters

Compare only the first 4 characters:

sort uh.txt | uniq -w 4
Comparing the first N characters

Ignore the letter case

uniq -i uh.txt

FAQ

Why do I need to sort first? uniq only sees duplicates that are next to each other. sort puts equal lines together. sort -u file is a shortcut that does both.

What is the difference between -d and -D? -d prints one copy of each repeated line. -D prints every copy.

How do I count the lines? Use uniq -c after sort.

How do I see lines that appear once? uniq -u.

Quick recap

  • sort file | uniq removes all duplicates, sort file | uniq -c counts them.