uniq Command in Linux

Learn via video courses
Topics Covered

Overview

The uniq command in Linux is a handy utility used for filtering out duplicate adjacent lines from text files or standard input, while preserving their order. It is commonly used for identifying unique occurrences, counting duplicates, and comparing files.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Linux uniq Command Syntax

The syntax for the uniq command is as follows:

Where:

  • OPTION: The flags and options that modify the behavior of the uniq command.
  • INPUT: The input file to be processed. If not specified, the standard input (stdin) is used.
  • OUTPUT: The output file where the results will be written. If not specified, the output is displayed on the standard output (stdout).

uniq Command Options:

  • -c, --count: Prefix lines by the number of occurrences.
  • -d, --repeated: Only print duplicate lines.
  • -u, --unique: Only print unique lines.
  • -i, --ignore-case: Ignore differences in case when comparing lines.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Example Usages

  • Remove duplicate adjacent lines from a text file.:

    Explanation: This command will remove duplicate adjacent lines from the 'input.txt' file and save the result in 'output.txt'.

  • Count the occurrences of each line in a file.:

    Output:

    Explanation: This command will count the occurrences of each line in the 'input.txt' file and display the result on the standard output.

Tips

  • Sort the input file before using the uniq command to get accurate results for non-adjacent duplicate lines.

Advanced Use Cases of uniq Command in Linux

  • Print only duplicate lines from a file.:

    Output:

    Explanation: This command will print only the duplicate lines from the 'input.txt' file on the standard output.

  • Print only unique lines from a file.:

    Output:

    Explanation: This command will print only the unique lines from the 'input.txt' file on the standard output.

  • Ignore case while comparing lines.:

    Output:

    Explanation: This command will ignore the case while comparing lines from the 'input.txt' file and display the result on the standard output.

Conclusion

  • The uniq command is used for filtering out duplicate adjacent lines.

  • It can count occurrences, print unique lines, or print duplicate lines.

  • The input can be a file or the standard input, and the output can be a file or the standard output.