Introduction to Diff Tool program usage for UNIX systems

Source: Internet
Author: User
Tags diff

You at the command line, enter:

$ diff < change before file > < changed file >

Diff will tell you what the difference is between the two files. Its display result is not very understood, below I will explain, how to read the diff.

bg2012082901

One, diff three kinds of formats

For historical reasons, diff has three different formats:

* Normal format (regular diff)

* Contextual format (context diff)

* Merge Format (Unified diff)

Let's look at it in turn.

Second, sample files

To make it easier to explain, create two sample files first.

The first file is called F1, and the content is one a for each line, a total of 7 lines.

A
A
A
A
A
A
A

The second file, called F2, modifies F1, the 4th line becomes B, and the other is unchanged.

A
A
A
B
A
A
A

Third, the normal format of the diff

Now compare the F1 with the F2:

$ diff F1 F2

At this point, diff displays the results of the normal format:

4c4
< a

> b

The first line is a hint to indicate the position of the change.

4c4

It is divided into three sections: the previous "4″" indicates that the 4th line of the F1 has changed; the middle "C" indicates that the change mode is the change of content, the other modes also include "Add" (A, representative addition) and "Delete" (d, Representative of deletion); The 4th line that turns into F2 after a change.

The second line is divided into two parts.

< a

The previous less-than sign, which means that the line is to be removed from the F1 (that is, line 4th), followed by "a" to indicate the contents of the line.

The third line is used to split F1 and F2.

Line four, similar to the second line.

> b

The previous greater-than number indicates that the row was added by F2, followed by "B" to indicate the contents of the line.

The first Unix (that is, at&t version of Unix) uses the diff in this format.

Four, the context of the format of the diff

When the University of California, Berkeley launched the BSD version of Unix in the early 80, it felt that the diff display was too simple to add to the context and make it easier to understand the changes that took place. As a result, the context format of the diff is introduced.

It is used by adding the C parameter (representing the context).

$ diff-c F1 F2

The results appear as follows:

F1 2012-08-29 16:45:41.000000000 +0800
-f2 2012-08-29 16:45:51.000000000 +0800
***************
1,7 * * * *
A
A
A
!a
A
A
A
-1,7--
A
A
A
!b
A
A
A

This result is divided into four parts.

The first part of the two lines shows the basics of two files: file name and time information.

F1 2012-08-29 16:45:41.000000000 +0800
-f2 2012-08-29 16:45:51.000000000 +0800

"* * *" indicates the document before the change, "-" to indicate the document after the change.

The second section is a 15 asterisk, separating the basic information of the file from the changes.

***************

The third part shows the document before the change, namely F1.

1,7 * * * *
A
A
A
!a
A
A
A

This shows not only the 4th row that has changed, but also the first three and three lines on line 4th, so 7 rows are displayed altogether. Therefore, the front of the "* * * * * * * * * * * 1,7, from line 1th 7 consecutive lines.

In addition, each line of the contents of the file is preceded by a bit of markup. If empty, indicates that the row has no change, if it is an exclamation point (!), indicates that the row has changed, if it is a minus sign (-), indicates that the row was deleted, and a plus sign (+) indicates that the behavior is new.

The forth part shows the changed document, namely F2.

-1,7--
A
A
A
!b
A
A
A

In addition to the Change row (line 4th), the context displays three rows, displaying a total of 7 rows.

Diff in merged format

If the similarity of two files is high, the diff in the context format will show a lot of duplicate content, which is a waste of space. GNU diff pioneered the "Merge format" diff in 1990, merging the context of F1 and F2.

It is used by adding the U parameter (representing unified).

$ diff-u F1 F2

The results appear as follows:

-F1 2012-08-29 16:45:41.000000000 +0800
+++ F2 2012-08-29 16:45:51.000000000 +0800
@@ -1,7 +1,7 @@
A
A
A
-A
+b
A
A
A

The first part of it is also the basic information of the document.

-F1 2012-08-29 16:45:41.000000000 +0800
+++ F2 2012-08-29 16:45:51.000000000 +0800

"-" means the document before the change, "+++" means the document after the change.

The second part, the position of change with two @ as the first and end.

@@ -1,7 +1,7 @@

The preceding " -1,7″" is divided into three parts: The minus sign represents the first file (i.e. F1), "1″ represents line 1th," 7″ indicates 7 consecutive rows. Together, it means that the first file is 7 consecutive lines starting at line 1th. Similarly, "+1,7″ represents a change, and becomes the second file 7 consecutive lines starting at line 1th."

The third part is the concrete content of the change.

A
A
A
-A
+b
A
A
A

In addition to the rows that have changed, the context shows 3 rows each. It displays the context of the two files, merged together, so it's called "Merge format." At the top of each line, the blank represents no change, the minus sign indicates the row that the first file deleted, and the plus sign indicates the new row for the second file.

The diff in git format

Version management system git, using a variant of the merge format diff.

$ git diff

The results appear as follows:

Diff–git a/f1 B/F1
Index 6f8a38c. 449b072 100644
-a/f1
+++ B/F1
@@ -1,7 +1,7 @@
A
A
A
-A
+b
A
A
A

The first line represents a diff that results in git format.

Diff–git a/f1 B/F1

The comparison is between a version of F1 (i.e. before the change) and a B version of the F1 (i.e., after the change).

The second line represents the two-version git hash value (the 6f8a38c object for the index region, compared to the 449b072 object in the Working directory area), and the last six digits are the object's schema (normal file, 644 permissions).

Index 6f8a38c. 449b072 100644

The third line represents the two files that are compared.

-a/f1
+++ B/F1

"-" to indicate the previous version of the change, "+++" to indicate the revised version.

The following lines are the same as the official merge format diff.

@@ -1,7 +1,7 @@
A
A
A
-A
+b
A
A
A

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.