Comparing files

Commands used to compare files.

Sorting files

sort -u file1 -o file1_sorted
sort -u file2 -o file2_sorted

comm

IMPORTANT: Files need to be sorted first for comm to work.

Compare two sorted files line by line. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.

man comm

Identical lines in both file1 and file2 (remove col 1 & 2)

comm -12 file1_sorted file2_sorted

Different lines from file1 and file2 (remove column 3)

comm -3 file1_sorted file2_sorted

diff

IMPORTANT: Files need to be sorted first for diff to work.

Compare files side by side (PREFERRED)

diff -y file1_sorted file2_sorted

Compare files (unified view, files combined)

– is for File1, + is for File2

diff -u file1_sorted file2_sorted

vimdiff

Use vi syntax (:q to quit).

Compare files side by side

vimdiff file1_sorted file2_sorted
d + o: gets changes from the other window into the current one
d + p: puts the changes from the current window into the other one
] + c: jumps to the next change
[ + c: jumps to the previous change
Ctrl + w: switches to the other split window

git

Compare character by character

git diff --word-diff=color --word-diff-regex=. file1.txt file2.txt

Visual Studio Code

  • Open both files in 2 tabs.
  • Select the 2 tabs (click + Shift), right-click -> Compare selected.