comm Command in Linux

Learn via video courses
Topics Covered

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.

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.

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.

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.