Comparing sorted file with comm

Given two sorted input files f1 and f2, comm prints three columns of output: lines that occur in f1 only, lines that occur only in f2 and lines that occur in both files. Any of these columns can be suppressed by an option.

With no options, comm produces three-column output. Column one contains lines unique to file f1, column two contains lines unique to file f2, and column three contains lines common to both files.

-1 suppress column 1 (lines unique to file f1)

-2 suppress column 2 (lines unique to file file f2)

-3 suppress column 3 (lines that appear in both files)

For example,

comm -12 f1 f2

prints lines in both files, and

comm -23 f1 f2

prints lines that are in the first file but not in the second. This is useful for comparing directories. Suppose we have two directories dir1 and dir2 that have many files in common but just a few differences. We are interested in the files that are in dir1 but not in dir2. Here is how to accomplish that.

[alice@localhost comm]: ls dir1
f1  f2  f3  f4  f5  f6  f8
[alice@localhost comm]: ls dir2
f1  f2  f3  f4  f5  f6  f7  f9
[alice@localhost comm]: ls dir1 > dir1.list
[alice@localhost comm]: ls dir2 > dir2.list
[alice@localhost comm]: comm -23 dir1.list  dir2.list
f8
[alice@localhost comm]: comm -13 dir1.list  dir2.list
f7
f9
[alice@localhost comm]: