Comparing files line by line with diff May 29th, 2018

diff [option] [files]

diff outputs the differences between two files, line by line. For files that are identical it produces no output, for binary files only reports if they are different or not.

The set of differences produced by diff is often called a diff or patch, and this output can be used later on by the patch command to change other files.

Before digging into the examples, let’s define two files that will help us to illustrate some concepts.

To keep the conceptual overhead as low as possible, the files are with-text.js and without-text.js to represent if the text variable is present or not.

File with-text.js :

function example ( text ) { console . log ( "====" ) console . log ( text ) }

File without-text.js :

function something () { console . log ( "====" ) }

By default, diff outputs a list of all the lines that are different between the files, one after the other, alongside with useful information about what changed.

A change can be:

An addition, represented by the letter a .

. A deletion, represented by the letter d .

. A change, represented by the letter c .

For every line in which diff finds a change, it outputs:

What type of change is (a/c/d)

What is the line number affected in each file

The content of the lines affected

In a more concrete example, if you run:

$ diff with-text.js without-text.js 1c1 < function example (text) { --- > function something () { 3d2 < console.log(text)

You get:

1c1 indicating that there’s a c hange in line 1 of both files

indicating that there’s a hange in line of both files < function example (text) { indicating how the line looks in with-text.js

indicating how the line looks in > function something () { indicating how the line looks in without-text.js

Are you able to deduce what the rest of the lines are expressing in the output?

Apart from the default output format, you can specify the --side-by-side (short -y ) to produce a side-by-side view of what changed.

This format is more visual and can be easier to understand at first sight, for example:

$ diff with-text.js without-text.js --side-by-side function example (text) { | function something () { console.log("====") console.log("====") console.log(text) < } }

A very powerful feature of diff is the ability to work with directories. It’s important to note that actual contents of directories are never compared as if they were a file, instead diff uses the following rules:

If one file is a directory and the other is not, diff compares the file in the directory whose name is that of the non-directory.

compares the file in the directory whose name is that of the non-directory. If two file names are given and both are directories, diff compares corresponding files in both directories, in alphabetical order.

Since version 3.4 (launched around August of 2016), diff supports the --color flag print colorised output in the terminal.

If you provide - as a file name, diff will use it for text read from the standard input. As a special case, diff - - compares a copy of standard input to itself.

diff also provides ways to suppress differences between files that may not be important to you, common examples of this are changes in the amount of white space between words or lines.

Flag Description -i --ignore-case Ignore case differences in file contents. --ignore-file-name-case Ignore case when comparing file names. --no-ignore-file-name-case Consider case when comparing file names. -E --ignore-tab-expansion Ignore changes due to tab expansion. -b --ignore-space-change Ignore changes in the amount of white space. -w --ignore-all-space Ignore all white space. -B --ignore-blank-lines Ignore changes whose lines are all blank. -I RE --ignore-matching-lines=RE Ignore changes whose lines all match RE.

While diff awesome, it’s not always the right tool for the job, here are some better fits for different scenarios:

The diff3 command can be useful to show differences among three files.

command can be useful to show differences among three files. For binary files, cmp reports the differences between two files byte by byte, instead of line by line.

reports the differences between two files byte by byte, instead of line by line. If you are looking only for visual diffing without the patching, vim -d and git diff do a good job.