diff (show differences between files)

This indispensable tool shows you the differences between two files that are similar but not quite the same. You'll mostly use it to find out if two files are identical or not, like this:

diff file1 file2

If file1 and file2 differ, diff will output some cryptic codes indicating the differences. Let's have a look at an example:

$ diff file1 file2

1a2

> hello

This means that the files' contents are identical, except that file2 contains the line “hello”, which file1 lacks. You can imagine the > as an arrow pointing towards file2. The code 1a2 is a bit more complicated. It's an editing code. To be able to interpret these codes, you should remember that they basically answer the following question:

“What do I have to do with file1 for it to become identical to file2?”

In this case, for file1 to become identical to file2, you have to go to the first line of file1 and append the second line of file2. That's what 1a2 stands for. There are only three editing codes that you need to remember:

  • a = append
  • c = change
  • d = delete

The program patch is used to apply the editing instructions produced by diff to a file.