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 fdisk Command: Inspect and Edit Disk Partitions

Linux fdisk Command: Inspect and Edit Disk Partitions

Learn how to inspect and Edit Disk Partitions with the Linux fdisk command, including practical examples, key options, and important precautions.

Linux command Command: Run Commands While Bypassing Aliases and Functions

Linux command Command: Run Commands While Bypassing Aliases and Functions

Learn how to use the Linux command command to check command availability and bypass shell aliases or functions, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux gzip Command: Compress a File in gzip Format

Linux gzip Command: Compress a File in gzip Format

Learn how to compress a File in gzip Format with the Linux gzip command, including practical examples, key options, and important precautions.

Linux df Command: Check Filesystem Free Space

Linux df Command: Check Filesystem Free Space

Learn how to check Filesystem Free Space with the Linux df command, including practical examples, key options, and important precautions.

Linux grep Command: Search Text and Filter Command Output

Linux grep Command: Search Text and Filter Command Output

Learn how to use the Linux grep command to search files for text or regular expressions and filter command output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux nmap Command: Scan Authorized Hosts and Ports

Linux nmap Command: Scan Authorized Hosts and Ports

Learn how to scan Authorized Hosts and Ports with the Linux nmap command, including practical examples, key options, and important precautions.

Linux whatis Command: Show One-Line Command Descriptions

Linux whatis Command: Show One-Line Command Descriptions

Learn how to use the Linux whatis command to display concise descriptions of commands and manual pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux lsmod Command: List Loaded Kernel Modules

Linux lsmod Command: List Loaded Kernel Modules

Learn how to list Loaded Kernel Modules with the Linux lsmod command, including practical examples, key options, and important precautions.

Linux Administration Memo: Essential Commands and System Checks

Linux Administration Memo: Essential Commands and System Checks

A compact Linux administration reference for checking system information, processes, storage, networking, users, permissions, services, and logs.

Linux ln Command: Create Hard Links and Symbolic Links

Linux ln Command: Create Hard Links and Symbolic Links

Learn how to create hard links and symbolic links with Linux ln, choose relative or absolute targets, replace links safely, and inspect link behavior.