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

Table of Contents

Compare text

diff is the most used command to compare text files. It will show the lines that are different between two files (you can also compare directories):

diff file1.txt file2.txt

Output will be something like this:

3c3
< 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.
  • -r: recursively compare subdirectories.
  • -y: print output in two columns.
  • -s: report when the files are identical.
  • -u <optional number>: show the context of each difference (by default, shows 3 lines of context).

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

You can also check if a file has been altered, comparing the hash values of the original file and the one you want to check.

If you have a properly formated MD5 or SHA1 checksum file, and the file you want to check is in the same folder as the checksum file, you can run this command to check whether the file has been altered:

md5sum -c some_file.iso.md5
sha1sum -c some_file.iso.sha1
  • You don’t need to create the checksum file with the .md5 or .sha1 suffix, but it’s a recommended practice naming the file the same as the main file, appending those suffixes.

For example:

$ md5sum -c Peropesis-1.6.2-live.iso.md5
Peropesis-1.6.2-live.iso: OK
  • OK means the file has not been altered (checksums match).

Checksum file content must follow this format (same as the output of md5sum <file> or sha1sum <file> commands):

<md5/sha1 sum> <filename>

For example:

5ec2dfb62a2e76067839ea10f269070e  Peropesis-1.6.2-live.iso

Compare audio files visually with Audacity

In Audacity, open the files (I am going to compare two audios). Select the second file and go to Effect -> Invert. Now, select both files and go to Tracks -> Mix -> Mix and Render to New Track. If the new track has no sound (a horizontal line), the audios are the same.

Find duplicated files

I have other posts about finding duplicated files:

Test with this online terminal:

If you have any suggestion, feel free to contact me via social media or email.