Linux uniq Command: Remove or Count Adjacent Duplicate Lines

uniq reduces consecutive identical lines to a single line or shows the number of repetitions. To remove duplicate values from the entire file, you usually need to sort first.

What is the uniq command?

Identical lines that are apart from each other cannot be removed using uniq alone. If you need to preserve the input order, do not sort blindly; first decide which duplicates matter. Using uniq after sorting will change the original line order.

Basic syntax

uniq [option] [input file [output file]]

The installed implementation and options may vary depending on the distribution. Check the description on your current system with man uniq.

Examples

Removing adjacent duplicates

When the same line occurs consecutively, only the first line is printed.

uniq events.txt

Counting all duplicates

After grouping identical values with sorting, use -c to display the number of repetitions.

sort names.txt | uniq -c

Check only duplicate values

Print the types of values that occur two or more times consecutively.

sort names.txt | uniq -d

Distinguishing adjacent duplicates from overall duplicates

uniq only combines consecutive identical lines. In the example below, the last apple is separate because it is not next to the first two lines.

printf 'apple\napple\nbanana\napple\n' | uniq -c

Example output (spaces before numbers may vary depending on implementation):

2 apple
1 banana
1 apple

To combine identical lines across the entire file into one, use sort file | uniq after sorting. However, sorting changes the original line order, so do not apply it blindly to logs or event records where the input order is important. -d shows only duplicate groups, and -u shows only groups that appear once.

Main Options and Format

Options/Format Description
-c Attach the count of consecutive occurrences in front of each output line.
-d Print only duplicate lines.
-u Print only lines that appear once.
-i Ignore case when comparing.
-f N Exclude the first N fields from comparison.

Precautions when using

The results of sorting and comparison with sort | uniq may vary depending on the locale. If you need an exact byte-based aggregation, apply the same LC_ALL=C environment to both commands. uniq -u refers to lines that appear only once, not all unique values.

Frequently Asked Questions

Why doesn’t uniq remove duplicate rows that are not consecutive?

Because it compares the current row with the immediately preceding row. To remove all duplicates, sort the input so that identical rows are grouped together.

Official Documentation

You can check the exact behavior of the options and implementation differences in the official uniq documentation.

More in This Category
Linux w Command: Show Logged-In Users and Their Processes

Linux w Command: Show Logged-In Users and Their Processes

Learn how to show Logged-In Users and Their Processes with the Linux w command, including practical examples, key options, and important precautions.

Linux cd Command: Change the Current Directory

Linux cd Command: Change the Current Directory

Learn how to use the Linux cd command to move between directories using absolute, relative, home, and previous paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux printenv Command: Print Environment Variable Values

Linux printenv Command: Print Environment Variable Values

Learn how to use the Linux printenv command to print all environment variables or selected variable values, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux cmp Command: Compare Files Byte by Byte

Linux cmp Command: Compare Files Byte by Byte

Learn how Linux cmp compares two files byte by byte, reports the first mismatch, runs silently in scripts, limits ranges, and differs from diff and checksums.

Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Learn how to remove or count adjacent duplicate lines with the Linux uniq command, including practical examples, key options, and important precautions.

Linux tmux Command: Detach and Reattach Terminal Sessions

Linux tmux Command: Detach and Reattach Terminal Sessions

Learn how to detach and Reattach Terminal Sessions with the Linux tmux command, including practical examples, key options, and important precautions.

Linux Tutorial / Inspect, Pause, and Stop Processes

Linux Tutorial / Inspect, Pause, and Stop Processes

Inspect Linux processes with ps, top, and pgrep, then safely pause, resume, and terminate a practice process while distinguishing shell jobs from services.

Linux chown Command: Change File Owner and Group

Linux chown Command: Change File Owner and Group

Learn how to change File Owner and Group with the Linux chown command, including practical examples, key options, and important precautions.

Linux Tutorial / File Permissions and Safe sudo Use

Linux Tutorial / File Permissions and Safe sudo Use

Learn to read file and directory permissions, practice chmod safely, and understand when to use sudo and how to diagnose access errors.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.