7.3 Comparing Files

The easiest way to check if two files are identical is cmp, which compares files byte by byte and complains at the first byte they differ.

To demonstrate, let’s first create two small files with identical content and then compare them with cmp:

$ echo "This file is unique" > a.txt
$ echo "This file is unique" > b.txt
$ cmp a.txt b.txt

Since these files are identical, cmp does not give any output.

Let’s next change one of the files a little and compare again:

$ echo "This file is not unique" > b.txt
$ cmp a.txt b.txt

Now we are told that these files differ at byte 14 on line 1. That is exactly where a.txt has a ‘u’ and b.txt has an ‘n’ (count from the first letter to check).