Have you ever had two similar files and wondered which parts are different? Are you writing code and trying to figure out how to blend in changes that you or some other programmer made? There's a standard Linux utility called "diff" that will help.

What Is diff?

diff is a utility that shows the differences between two text files, which is where the name comes from. It's part of the POSIX standard, which means that it's present on nearly all Unix-like systems, including Linux as well as macOS and the other BSDs.

If you're on a major Linux distribution, you'll most likely have the GNU version. If you're on a minimal Linux distro, you might have it as part of the BusyBox or Toybox library or the BSD version if you're on a BSD system. You can see which version you have by checking the manual page:

        man diff
    

Comparing Two Files on Linux With diff

Output from diff command

To compare two text files, simply pass their path as arguments to diff:

        diff file1 file2
    

diff will show the lines that are different between the two in the file. Lines in the first file are prefaced with a "<" and lines in the second are preceded with a ">" character. The output displays changed, appended, or deleted lines, including the number of lines affected overall.

If you change the first line, diff will output "1c1", which means "start at line 1, change line 1." In diff, "a" stands for "append", "c" for "change" and "d" for "delete."

Other diff Features and Options

To see files side-by-side, use the -y option:

        diff -y file1 file2
diff -y command output

The -u option produces a "unified" outlook with an editing script for the ed editor so that changes can be performed automatically.

diff -u output

Before distributed version control systems like Git were widely used, open-source projects would solicit patches in this format by email and then apply them to the codebase with the patch program.

Now You Can Compare Files on Linux With diff

The diff utility may seem obscure, but it's beneficial to see the differences between two files, whether you're simply comparing the files or submitting patches to an open-source developer.

If you're not a fan of the Linux terminal, you don't even have to use the command line to compare files, or even use your local machine at all. There are online tools that will let you compare files on the web.