Title: Comparing Files (cmp, diff and comm)
Course: CIS 121: Linux/UNIX I: Fundamentals ~ For IGNITE Review > Section: Learning Modules

cmd

The cmd command will compare two files and report the position of the first difference, if one exists.

Example:  cmd file2 file2

Will report the position of the first difference or nothing if the files are the same.

diff

The diff command will compare two files and list the differences.

Example: diff file1 file2

Will list all of the differences between file1 and file2 by showing how file1 should be modified to become identical to file2. Below are some sample output of the diff command.

- 1d0 (Delete first line)
- < Boo
- 3c2 (Change line 3 to match line 2 of the second file)
- < file1
- ---
- > file2
- 4a4 (Add a new line 4)
- > hello

comm

The comm command can be used to find lines that are unique to one file, unique to the other file, or in common to both.  Both files must be sorted.

- It creates three columns. The first contains what is unique to the first listed file, the second what is unique to the second listed file, and the third what is in common to both.
- The comm command creates columns two and three by placing a tab or two tabs at the front of the line.
- You can take away (drop or remove) the columns with options:
  - -1 (Take away column one - unique to file1)
  - -2 (Take away column two - unique to file2)
  - -3 (Take away column three - common lines)

Below are some examples:

- comm fileA fileB
  - Standard three column output.
- com -3 fileA fileB
  - Drop the "in common" lines.
- comm -23 fileA fileB
  - Show only what is unique to fileA.
- comm -12 fileA fileB
  - Show only what is in common to both.