uniq Command — Removing Duplicate Lines in Linux¶
The
uniqcommand is used to identify, remove, and count duplicate lines in text files. It is commonly used after thesortcommand to clean data, generate reports, analyze logs, and process large datasets. Every Linux Administrator, DevOps Engineer, and Cloud Engineer should know how to useuniqeffectively.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
uniqcommand - Remove duplicate lines
- Count duplicate entries
- Display only duplicate lines
- Display only unique lines
- Combine
uniqwithsort - Analyze production logs and reports
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 Lessons 1–4
Why Learn uniq?¶
Imagine you have a log file containing repeated error messages.
Instead of manually identifying duplicates:
Output:
This is one of the most common Linux data-cleaning operations.
What is uniq?¶
The uniq command filters or reports repeated lines in a text file.
Important: uniq only works on adjacent duplicate lines.
For reliable results, always sort the file first.
Syntax:
Sample File¶
Create a file.
Contents:
Press:
Remove Duplicate Lines¶
Output:
Why sort is Important¶
Consider:
Command:
Output:
The duplicate Apple is not removed because the duplicate lines are not adjacent.
Correct approach:
Output:
Count Duplicate Lines¶
Output:
The -c option prefixes each line with the number of occurrences.
Display Only Duplicate Lines¶
Output:
Only repeated values are displayed.
Display Only Unique Lines¶
Output:
Only lines that appear once are displayed.
Ignore Case¶
Create:
Command:
Treats uppercase and lowercase letters as identical.
Skip Characters¶
Suppose:
Ignore the first four characters.
Useful for fixed-width reports.
Skip Fields¶
Example:
Command:
Ignores the first field while comparing lines.
Combining with Pipes¶
Remove duplicate users.
Count duplicate IP addresses.
Display unique departments.
Common uniq Options¶
| Option | Description |
|---|---|
-c | Count duplicate lines |
-d | Display only duplicates |
-u | Display only unique lines |
-i | Ignore case |
-f | Skip fields |
-s | Skip characters |
Real Production Examples¶
Count failed login attempts.
Find duplicate IP addresses.
Display unique Docker images.
Count Kubernetes namespaces.
Generate unique usernames.
Production Perspective¶
The uniq command is commonly used for:
- Cleaning reports
- Removing duplicate records
- Counting repeated log entries
- Security log analysis
- Audit reporting
- Data preprocessing before scripting
It is almost always used together with sort.
Hands-on Lab¶
Task 1¶
Create:
Contents:
Task 2¶
Sort the file.
Task 3¶
Remove duplicates.
Task 4¶
Count duplicates.
Task 5¶
Display duplicates only.
Task 6¶
Display unique entries only.
Task 7¶
Count unique usernames.
Task 8¶
Count duplicate shells.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
uniq | Remove adjacent duplicates | Reports |
uniq -c | Count duplicates | Log analysis |
uniq -d | Show duplicates | Security audits |
uniq -u | Show unique values | Data validation |
uniq -i | Ignore case | User reports |
uniq -f | Skip fields | Fixed-format reports |
Production Troubleshooting Scenario¶
Scenario
A web server's access log contains repeated client IP addresses.
Tasks:
- Display unique client IPs.
- Count how many requests came from each IP.
- Display only duplicate IP addresses.
- Identify IPs that appear only once.
Solutions:
cut -d " " -f1 access.log | sort | uniq
cut -d " " -f1 access.log | sort | uniq -c
cut -d " " -f1 access.log | sort | uniq -d
cut -d " " -f1 access.log | sort | uniq -u
Mini Challenge¶
Create:
Contents:
Perform the following:
- Sort the file.
- Remove duplicate names.
- Count each name.
- Display duplicate names only.
- Display names that appear only once.
- Count the total number of unique employees.
Best Practices¶
- Always sort data before using
uniq. - Use
uniq -cto generate quick frequency reports. - Combine
uniqwithcutandgrepfor advanced analysis. - Use
uniq -uto identify unique records. - Use
uniq -dto detect duplicate entries.
Common Mistakes¶
❌ Using uniq on unsorted data.
✅ Incorrect:
Correct:
❌ Assuming uniq removes all duplicates automatically.
✅ It only removes adjacent duplicate lines.
❌ Forgetting to sort before counting duplicates.
✅ Always use:
Interview Questions¶
Beginner¶
- What is the purpose of the
uniqcommand? - Why is
sortcommonly used beforeuniq? - What does
uniq -cdo? - What is the difference between
uniq -danduniq -u?
Intermediate¶
- Explain why
uniqonly removes adjacent duplicates. - How do you count duplicate IP addresses in a log file?
- What does
uniq -ido? - When would you use
uniq -f?
Architect Level¶
- How would you analyze duplicate log entries in a production environment?
- Why is
uniquseful for security and audit reporting? - How would you combine
cut,sort, anduniqto generate a summary report?
Summary¶
In this lesson, you learned:
- Removing duplicate lines
- Counting duplicate entries
- Displaying only duplicate or unique lines
- Combining
uniqwithsort - Production log analysis
- Data cleaning techniques
The uniq command is a simple but powerful utility for cleaning and summarizing data. When combined with sort, it becomes an essential tool for Linux administration and text processing.
Key Takeaways¶
uniqremoves adjacent duplicate lines.- Always sort data before using
uniq. uniq -ccounts occurrences.uniq -ddisplays only duplicate lines.uniq -udisplays only unique lines.uniqis commonly used for log analysis, reporting, and data cleanup.
What's Next?¶
tr Command — Translating and Transforming Text in Linux
In the next lesson, you'll learn:
- Character translation
- Converting lowercase to uppercase
- Replacing characters
- Deleting characters
- Squeezing repeated characters
- Real-world text transformation examples