Learn how to compare two files with a terminal using several methods.

Compare text

diff is the most used command to compare text files. It will show the lines that are different between two files:

diff file1.txt file2.txt

Output will be something like this:

< daemon:x:2:2::/:/usr/bin/nologin
> aemon:x:2:2::/:/usr/bin/nologin
  • First line shows where is the change in each file, and what type of change is:
    • 3: line 3 of first file.
    • c: it’s a replace change. “d” means the line has been deleted. “a” means the line has been added.
    • 3: line 3 of second file.
  • In next lines < means the first file and > the second file.

diff has some options:

  • -i: ignore case.
  • -b: ignore changes in the amount of white space.
  • -q: report only if files differ, not where they differ.
  • -y: print output in two columns.

Compare byte to byte

You can compare binary or text files using hash functions. These functions map data of any size to a fixed-size value (hash value). If two files have the same hash value, this means that the files are identical. Most used hash functions are MD5 and SHA1, and the commands you can use to compare files are md5sum and sha1sum.

$ md5sum pass1.txt pass2.txt 
b7313aea7fb19a8e10fc218db11ad59e  pass1.txt
fe9e5e426d6bc37964143a6d98720992  pass2.txt
$ sha1sum pass1.txt pass2.txt 
ffb77ff2f8803997bed44ae8ea055c89061b231c  pass1.txt
86ff88da01d6f78b034f705638e1d1847e5124d0  pass2.txt