The Common Options for the Linux comm
Command
The comm
command in Linux is a powerful tool used to compare two sorted text files and display the lines that are unique to each file, as well as the lines that are common to both files. Here are the common options for the comm
command:
-
Basic Usage:
comm file1.txt file2.txt
: Compares the contents offile1.txt
andfile2.txt
and displays the lines that are unique to each file, as well as the lines that are common to both files.
-
Comparison Options:
-1
: Suppresses the display of the lines that are unique to the first file.-2
: Suppresses the display of the lines that are unique to the second file.-3
: Suppresses the display of the lines that are common to both files.
-
Output Options:
-i
: Ignores case when comparing the lines.-u
: Prints only the unique lines from both files, without the column markers.
-
Formatting Options:
-1
: Displays the lines that are unique to the first file in the first column.-2
: Displays the lines that are unique to the second file in the second column.-3
: Displays the lines that are common to both files in the third column.
Here's a Mermaid diagram to visualize the different options:
Now, let's look at some examples to better understand the usage of the comm
command:
-
Comparing Two Files:
$ comm file1.txt file2.txt
This will display the lines that are unique to
file1.txt
, the lines that are unique tofile2.txt
, and the lines that are common to both files. -
Suppressing Unique Lines:
$ comm -3 file1.txt file2.txt
This will display only the lines that are common to both
file1.txt
andfile2.txt
. -
Ignoring Case:
$ comm -i file1.txt file2.txt
This will compare the contents of
file1.txt
andfile2.txt
while ignoring case differences. -
Printing Unique Lines:
$ comm -u file1.txt file2.txt
This will display only the unique lines from both
file1.txt
andfile2.txt
, without the column markers.
The comm
command is a versatile tool that can be used in various scenarios, such as comparing configuration files, finding differences between database backups, or even analyzing log files. By understanding the different options available, you can effectively use the comm
command to meet your specific needs.