comm

file comparisonLinux/Unix
The comm command is one of the most frequently used commands in Linux/Unix-like operating systems. comm Compare two sorted files line by line

Quick Reference

Command Name:

comm

Category:

file comparison

Platform:

Linux/Unix

Basic Usage:

comm [options] [arguments]

Common Use Cases

    Syntax

    comm [OPTION]... FILE1 FILE2

    Options

    Option Description
    -1 Suppress lines unique to FILE1
    -2 Suppress lines unique to FILE2
    -3 Suppress lines that appear in both files
    --check-order Check that the input is correctly sorted, even if all input lines are pairable
    --nocheck-order Do not check that the input is correctly sorted
    --output-delimiter=STR Separate columns with STR
    --total Output a summary
    -z, --zero-terminated Line delimiter is NUL, not newline
    --help Display help message and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the comm command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    # Show lines unique to file1, unique to file2, and common to both
    comm file1.txt file2.txt
    # Show only lines unique to file1 comm -23 file1.txt file2.txt
    # Show only lines unique to file2 comm -13 file1.txt file2.txt
    # Show only lines common to both files comm -12 file1.txt file2.txt

    Advanced Examples:

    # Use with sort to ensure input is sorted
    sort file1.txt > sorted1.txt
    sort file2.txt > sorted2.txt
    comm sorted1.txt sorted2.txt
    # Compare files with case insensitivity sort -f file1.txt | comm -12 - <(sort -f file2.txt)
    # Combine with other utilities sort file1.txt file2.txt | uniq -d | comm -12 - <(sort file1.txt) # Compare word lists and redirect outputs to separate files comm -1 <(sort wordlist1.txt) <(sort wordlist2.txt) > only_in_wordlist2.txt comm -2 <(sort wordlist1.txt) <(sort wordlist2.txt) > only_in_wordlist1.txt comm -3 <(sort wordlist1.txt) <(sort wordlist2.txt) > common_words.txt

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The comm command compares two sorted files line by line and outputs three columns: lines unique to the first file, lines unique to the second file, and lines common to both files.

    Key points about comm:

    • Input files MUST be sorted for comm to work correctly
    • By default, the output is in three columns separated by tabs
    • Column 1: Lines unique to FILE1
    • Column 2: Lines unique to FILE2
    • Column 3: Lines common to both files
    • Options -1, -2, and -3 suppress the display of the respective columns
    • Can use - for standard input instead of a file

    Common use cases:

    • Finding differences and similarities between two lists
    • Comparing text files that have been sorted
    • Identifying common elements between two sets of data
    • Creating set operations (union, intersection, difference) on text files
    • Finding unique or duplicate lines between files

    Tips for using comm effectively:

    • Always sort input files first: sort file1.txt > sorted1.txt
    • Use process substitution in bash for inline sorting: comm <(sort file1.txt) <(sort file2.txt)
    • Combine options to isolate specific data: comm -23 = only lines unique to first file
    • Use --output-delimiter to make output more readable or easier to parse
    • For binary files or non-text data, consider using cmp or diff instead

    Unlike diff which shows how to change the first file to match the second file, comm simply shows which lines are unique or common to both files.

    Related Commands

    These commands are frequently used alongside comm or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the comm command works in different scenarios.

    $ comm
    View All Commands