comm Command in Linux

Learn via video courses
Topics Covered

Build an AI-First Career, Master the Complete Skillset

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
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

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

Overview

The comm command in Linux is an essential utility that aids in comparing two sorted files line by line. This command, while seemingly simple, can provide a lot of power when handling data files. The primary purpose of the comm command is to report the lines that are common, and those that are unique, in the two compared files.

Syntax of comm Command in Linux

The basic syntax of the comm command in Linux is as follows:

Where:

  • comm: This is the command itself.
  • OPTIONS: These are the flags that modify the behavior of the command.
  • FILE1 FILE2: These are the two sorted files that are to be compared.
Sharpen Your Fundamentals with Free Learning

Options in comm Command in Linux

  1. -1: Suppresses the output column of lines unique to file1.

    For example -

    Output:

    This command omits the lines that are unique to file1.

  2. -2: Suppresses the output column of lines unique to file2.

    For example -

    Output:

    This command omits the lines that are unique to file2.

  3. -3: Suppresses the output column of lines that appear in both files.

    For example -

    Output:

    This command omits the lines that are common to both files.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example Usages

  • Comparing two files without any options.:

    Output:

    Explanation: This basic usage of the comm command in Linux shows the unique and common lines between two files.

  • Comparing two files while suppressing lines unique to file1.:

    Output:

    Explanation: This command omits the lines that are unique to file1.

Tips

  • Ensure the files being compared are sorted. If they are not, the comm command in Linux may not work as expected.

  • You can combine options. For example, 'comm -12' shows only the lines common to both files.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Advanced Use Cases of comm Command in Linux

  • Filtering common lines into a new file.:

    Output:

    Explanation: This command uses redirection to store the common lines from file1 and file2 into a new file named common.txt.

  • Using comm in conjunction with sort to compare unsorted files.:

    Output:

    Explanation: This command first sorts the files before passing them to comm, allowing you to compare files that were initially unsorted.

  • Using comm with pipes and other commands like sort and uniq.:

    Output:

    Explanation: This command chain sorts the files, removes non-duplicated lines with uniq, and then uses comm to display lines that are common in the output and in file1.

Conclusion

  • The comm command in Linux is a powerful utility for comparing two sorted files line by line.

  • It can be used with various options to suppress or display lines unique to each file and lines common to both files.

  • The comm command can be used in combination with other commands and utilities in Linux for more advanced file comparisons and manipulations.