comm Command in Linux
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreOverview
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.
Options in comm Command in Linux
-
-1: Suppresses the output column of lines unique to file1.
For example -
Output:
This command omits the lines that are unique to file1.
-
-2: Suppresses the output column of lines unique to file2.
For example -
Output:
This command omits the lines that are unique to file2.
-
-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
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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
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.