--- layout: page author: Alessandro Luini category-page: advanced category-title: Advanced commands tags: difference lines diff file separating title: diff ---

diff analyzes two files and prints the lines that are different. Essentially, it outputs a set of instructions for how to change one file to make it identical to the second file.

How to use

To activate the diff command, we must take as parameters two or more files that we want to compare and it will give us the output of different lines.
For example let's say we have two files, file1.txt and file2.txt.

If file1.txt contains the following four lines of text:

  1. I need to buy apples.
  2. I need to run around the park.
  3. I need to wash the dog.
  4. I need to get the car detailed.

...and file2.txt contains these four lines:

  1. I need to buy apples.
  2. I need to do the laundry.
  3. I need to wash the dog.
  4. I need to get the dog detailed.

...then we can use diff to automatically display for us which lines differ between the two files with this command:

diff file1.txt file2.txt

...and the output will be:

 2,4c2,4 
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
---
> I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.

In our output above, "2,4c2,4" means: "Lines 2 through 4 in the first file need to be changed to match lines 2 through 4 in the second file.

Flags