Linux text Contrast diff command detail (collation)

Source: Internet
Author: User
Tags diff

diff command explanation

1. Overview

There are good text comparison tools available under the Windows system, such as the common beyond Compare,winmerge is a comparison tool for graphical interfaces and very handy, and if you just work under Windows, these GUI comparison tools are definitely the first choice. For those who work in a Linux environment, it is too much trouble to download files to the Windows environment and compare them with graphical tools every time you want to see the difference between the two files. Then we have to learn to use diff tools in the Linux environment.

The diff command compares a given two files in the simplest case. If you use "-" instead of the "file" parameter, the content to be compared will come from standard input. The diff command compares the similarities and differences of text files in a row-by-line manner. If the command specifies a comparison of the directory, the file with the same file name in the directory will be compared without any comparison of its subdirectory files.

diff (options) (parameters)
-<: Specifies the number of lines of text to display. This parameter must be used in conjunction with the-C or-u parameter;-A or--text:diff presets will only compare text files line by row;-B or--ignore-space-change: Do not check for differences in whitespace characters ;-B or--ignore-blank-lines: Do not check for blank lines;-C: Displays all contents and marks the difference;-c< number of lines > or--context<;: Same as execute "-c-< lines >";-D or- -minimal: Use different algorithms to make comparisons in small units;-d< macro name > or ifdef< macro name;: The output format of this parameter can be used for the predecessor processor macro;-e or--Ed: The output format of this parameter can be used for Ed script file,-F or-forward-ed: The output format is similar to Ed's script file, but it shows different places in the order of the original file;-H or--speed-large-files: When comparing large files, Speeds up;-l< characters or strings > or--ignore-matching-lines< characters or strings;: If two files are different in a few lines, but the same time the flight contains the character or string specified in the option, the difference between the two files is not displayed. ;-I or--ignore-case: Do not check case differences;-L or--paginate: Leave the resultPR program to pagination;-N or--rcs: Displays the comparison results in RCS format;-N or--new-file: When comparing a directory, if file a only appears in a directory, the preset will display: Only in directory, file a if using the-n parameter, diff will compare file A with a blank file ;-P: If the file is a C code file, the function name of the difference is displayed;-P or--unidirectional-new-file: similar to-N, but only if the second directory contains a file that is not in the first directory, the file is compared to a blank file ;-Q or--brief: Show only the difference, do not display detailed information;-R or--recursive: compare files in subdirectories;-S or--report-identical-files: If no differences are found, the information;-s< file is still displayed > or--starting-file<;: When comparing directories, compare from specified file,-T or--expand-tabs: When output, the tab character expands ;-T or--initial-tab: The tab character is prepended to each line to align the number of;-u,-u< columns > or--unified=< columns;: Displays the different contents of the file in a merged manner;-V or--version: Displays version information; w or--ignore-all-space: ignores all whitespace characters;-w< width > or--width< width;: Specifies the width of the column when using the-y parameter;-X < file name or directory > or--exclude< file name or directory;: Do not compare files or directories specified in options;-x< files > or--exclude-from< files >; You can save a file or directory type as a text file , and then specify this text file in the =< file >; Y or--side-by-side: Displays the similarities and differences of the files in a side-by-side way;--help: Show Help ;--left-column: When using the-y parameter, if one row of two files is the same, the contents of the row are displayed only in the left column;--suppress-common-lines: When you use the-y parameter, only the differences are displayed. 

There is also a Colordiff command that identifies different places with color. Need to install first

How 2.diff works, how to understand the results of diff execution

Diff parses two files and outputs a different line of two files. the output of diff shows how a file needs to be manipulated before it can be matched to the second file "or so: the second file has changed from the first file to" Theresult output of git diff". diff does not change the contents of the file, but diff can output an ed script to apply these changes.
Now let's take a look at how diff works, assuming there are two files:

I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.

I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

We use diff to compare their differences:
Diff File1.txt File2.txt

The following results are output:
< 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.

Let's explain the meaning of the output, and to understand the meaning of the diff comparison, we have to keep in mind that the difference between describing two files in diff is telling us how to change the first file to match the second one. Let's take a look at the first line in the comparison above 2,4c2,4 the preceding number 2,4 represents the line in the first file, with a letter C representing the action to be done on the first file (A=add,c=change,d=delete), followed by a number 2,4 representing the row in the second file.

The meaning of the 2,4c2,4 is that the first [2,4] line in a file (note that this is a closed interval, including lines 2nd and 4th) needs to be modified to match the [2,4] row in the second file.
The next part tells us what needs to be modified, the section of the front with < represents the contents of the [2,4] line of the left file, and the > section represents the contents of the [2,4] line of the right file, and the middle---is the separator symbol for two file contents.

3.Normal Mode

The above section we show how to view the results of the diff command, in fact, for the above comparison, we are using the diff command of the normal mode, which is the default mode of the diff command, that is, when the diff two files if the non-modal parameter is the default mode for comparison, its effect and (--normal), we give some examples to illustrate the output in normal mode (a comparison has been shown earlier), in order to visually see the difference between the two files I am under Windows through the Beyondcompare tool to list two files of different, The black part below is the diff output.

The first file has fewer rows than the second file:

The first file is more travel than the second file:

The first file is not the same as the second file:

The diff command above does not specify an additional mode parameter, so it uses the default normal mode, and the effect is the same as adding command-line arguments--normal.

4.Context Mode

By default the mode output is actually in line with the computer's thinking mode, not too intuitive, so its output is not very well understood, the diff command in addition to the default mode provides two other modes, context and unified mode, In this section we'll talk about how to check the output in context mode. The diff command applies the context mode only to add command line arguments diff-c, let's take a look at two case files:

The following is a description of the results of the two files compared in diff-c way:

5.Unified Mode

Diff also has a comparison, that is, the unified mode, using the command line –u to perform the comparison of the pattern. The comparison is similar to the context pattern, but simplifies some of the output, let's look at our case file, as in the above:

Results from using Diff–u comparison:

You can see that the comparison results are virtually the same as the context pattern, except that the comparison results are merged together.

6. Compare Catalogs

Use diff to compare two directories, compared to the diff directory1 Directory2 to see the following table of contents comparison results:

When comparing two directories, there are only a few files that exist in one directory and none in the other, and if there is a file with the same name, the difference between the two files is compared. Diff Compare the results of the catalog we can combine the grep command to filter out the output that we want, such as just outputting a different file in two directories and ignoring the output record that a directory is unique to another directory that does not exist.

7. Parameters- E Saves the results of the comparison as an Ed script, after which the ED program can execute the script file, thus modifying the file1 to be the same as the file2 content, which is generally useful in patch.

Diff-e 1.txt 2.txt > Script.txt
This is the creation of an ED can execute script file Script.txt, after generating the script file we also need to do an operation, add the Ed's write instruction at the end of the script file, only need to execute echo "W" >>script.txt Append the w instruction to the last line of the script file.
So how to apply the script file, you can use this:
Ed-1.txt < Script.txt
Note the middle – the symbol indicates reading from the standard input, while < script.txt redirects the script.txt content to the standard input. After doing so, the 1.txt content will be exactly the same as 2.txt.

Excerpt from: Http://


Linux text Contrast diff command detail (collation)

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: 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.