Skip to content

comm Linux Command Guide

The Linux comm command is a powerful tool for comparing sorted files line by line. It can display lines that are unique to each file or common to both. Use comm to easily identify differences and similarities between text files. By default, comm displays three columns: lines unique to the first file, lines unique to the second file, and lines common to both. It also offers options to customize the output and suppress certain columns. Master comm to streamline your file comparison tasks efficiently.

comm Syntax:

Terminal window
comm [option]... file1 file2

Options:

OptionDescription
-1Suppress lines unique to file1
-2Suppress lines unique to file2
-3Suppress lines that appear in both files

Parameters:

ParameterDescription
file1The first file to compare
file2The second file to compare

comm Command Samples:

Compare two sorted files

Terminal window
comm file1.txt file2.txt

Compares the sorted files “file1.txt” and “file2.txt” and displays three columns: lines unique to file1, lines unique to file2, and lines common to both files.

Suppress the first column

Terminal window
comm -1 file1.txt file2.txt

Compares the files “file1.txt” and “file2.txt” and displays only the lines unique to the second file and lines common to both files, suppressing the first column.

Suppress the second column

Terminal window
comm -2 file1.txt file2.txt

Compares the files “file1.txt” and “file2.txt” and displays only the lines unique to the first file and lines common to both files, suppressing the second column.

Suppress the third column

Terminal window
comm -3 file1.txt file2.txt

Compares the files “file1.txt” and “file2.txt” and displays only the lines that are unique to each file, suppressing the third column showing lines common to both files.

Ignore leading whitespace

Terminal window
comm -w file1.txt file2.txt

Compares the files “file1.txt” and “file2.txt” while ignoring any leading whitespace on each line.

Output only lines unique to both files

Terminal window
comm -3 -1 -2 file1.txt file2.txt

Compares the files “file1.txt” and “file2.txt” and displays only the lines that are unique to both files, excluding the common lines.

Suppress all columns

Terminal window
comm -123 file1.txt file2.txt

Compares the files “file1.txt” and “file2.txt” and suppresses all three columns, displaying no output by default.

comm FAQ:

How do I use comm in Linux?

To use the comm command in Linux, execute the following command:

Terminal window
comm --option <value>

How can I compare two sorted files line by line using comm?

To compare two sorted files line by line in Linux, you can use the comm command as shown below:

Terminal window
comm file1.txt file2.txt

How do I display lines that are unique to the first file only using comm?

To display lines that are unique to the first file only in Linux using comm, you can run the following command:

Terminal window
comm -23 file1.txt file2.txt

How can I suppress specific columns in the output when using comm in Linux?

To suppress specific columns in the output of the comm command in Linux, you can use the -1, -2, or -3 options as shown below:

Terminal window
comm -23 file1.txt file2.txt -1

How do I show lines that are common to both files using comm in Linux?

To display lines that are common to both files in Linux using comm, you can use the following command:

Terminal window
comm -12 file1.txt file2.txt

How can I ignore case sensitivity when comparing lines with comm in Linux?

To ignore case sensitivity when comparing lines using the comm command in Linux, you can utilize the -i option as shown below:

Terminal window
comm -i file1.txt file2.txt

Applications of the comm command

  • Comparing two sorted files line by line
  • Finding lines that are unique to the first file
  • Finding lines that are unique to the second file
  • Finding lines that are common in both files