Compare Two Files Using Comm

by Ramesh

Comm command is used to compare two sorted files line by line.

  1. syntax: comm [OPTION]... FILE1 FILE2

This command will display the lines unique in file1, lines unique in file2, and lines common to both file1 and file2 as shown below.

In the below example,

  • First column displays the lines unique in file1 ( i.e name_list.txt )
  • Second column displays the lines unique in file2 ( i.e name_list_new.txt )
  • Third column displays the lines that are common in both the files.
  1. $ cat name_list.txt
  2.  
  3. Bram Moolenaar
  4. Ken Thompson
  5. Linus Torvalds
  6.  
  7. $ cat name_list_new.txt
  8.  
  9. Bram Moolenaar
  10. Dennis Ritchie
  11. Richard Stallman
  12.  
  13. $ comm name_list.txt name_list_new.txt
  14. Bram Moolenaar
  15. Dennis Ritchie
  16. Ken Thompson
  17. Linus Torvalds
  18. Richard Stallman