Comparing files using the diff command

The diff command 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.

Usage

Here’s the syntax for the diff command.

diff [option] [file1] [file2]

Here’s an example of two files:
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("====")

Output

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 change in line 1 of both files.
  • < function example (text) { indicating how the line looks in with-text.js.
  • > function something () { indicating how the line looks in without-text.js.
Side by side comparison

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)           <
}                           }

Working with directories

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.
  • If two file names are given and both are directories, diff compares corresponding files in both directories, in alphabetical order.
Omitting differences

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.

 


– masterkenneth

Leave a Reply

Your email address will not be published. Required fields are marked *