Harnessing `comm` for Precise File Comparison

Quick Tip

Harnessing `comm` for Precise File Comparison

Challenge: You need to find lines that are unique to one file, unique to another, or common to both, without relying on complex `diff` output or manual `grep`ing.

The Solution: The `comm` command allows you to compare two sorted files line by line and output three columns: lines unique to file1, lines unique to file2, and lines common to both.

comm -12 file1.txt file2.txt # Lines common to both files comm -23 file1.txt file2.txt # Lines unique to file1 comm -13 file1.txt file2.txt # Lines unique to file2

Why it works: `comm` assumes the input files are sorted. The flags `-1`, `-2`, and `-3` suppress the output of the first, second, and third columns respectively, allowing you to isolate the specific comparison results you need.

Pro-Tip: Use `sort file1.txt > sorted_file1.txt` and `sort file2.txt > sorted_file2.txt` before running `comm` if your files are not already sorted.

Linux Tips & Tricks | © ngelinux.com | 4/26/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments