Linux text comparison command: diff

Source: Internet
Author: User

Linux text comparison command: diff

The diff command is an important tool in Linux. It is used to compare the content of files, especially files of different versions, to find the changes. Diff prints the changes of each line in the command line. The latest diff version also supports binary files. The output of the diff program is called a patch. Because there is a patch program in the Linux system, the content of the. c file can be updated to B. c according to the diff output. Diff is an indispensable part of version control tools such as svn, cvs, and git.

Command Format:

Diff [parameter] [file 1 or directory 1 Before change] [file 2 or directory 2 after change]

Command function:

The diff command can compare the content of a single file or directory. If you specify a file to be compared, it is valid only when the input is a text file. Compare the similarities and differences of text files in line-by-line mode. If a directory is compared, the diff command compares two text files with the same name. List different binary files, public subdirectories, and files that only appear in one directory.

Command parameters:

-Y or -- side-by-side displays Similarities and Differences of files in parallel.

-W or -- width specifies the column width when the-y parameter is used.

-C: display all internal text and mark the differences.

-U,-U, or -- uniied = are combined to display different file content.

-R or -- recursive compares files in the subdirectory.

-N or -- new-file when comparing directories, if file A Only appears in A directory, the default is: Only in Directory: If file A uses the-N parameter, then, diff compares file A with A blank file.

-B or -- ignore-space-change do not check the difference of space characters

-B or -- ignore-blank-lines do not check blank lines.

-H or -- speed-large-files can accelerate the speed of large files.

-L or -- ignore-matching-lines. If the two files are in different lines, these lines both contain the character or string specified in the option, the differences between the two files are not displayed.

-I or -- ignore-case do not check the case sensitivity.

-Q or -- brief only shows no difference and does not show detailed information.

-R or -- recursive compares files in the subdirectory.

-S or -- report-identical-files are still displayed if no difference is found.

-S or -- starting-file: when comparing directories, compare them from the specified file.

-T or -- expand-tabs expand the tab character in the output.

-T or -- initial-tab Add a tab character before each line for alignment.

-W or -- ignore-all-space ignores all space characters.

-V or -- version displays version information.

Instance

[Root @ bkjia src] # cat a.txt
111
Aaa
AAA
111aaa
Aaa
AAA
111
[Root @ bkjia src] # cat B .txt
111
Aaa
AAA
111AAA
Aaa
AAA
111
[Root @ bkjia src] #

Normally formatted diff is as follows:

1 <span style = 'margin: 0px; padding: 0px; color: rgb (102,102,102); line-height: 26px; text-indent: 26px; font-family: "", arial; font-size: 16px; '> [root @ bkjia src] # diff a.txt B .txt <br> 4c4 <br> <111aaa <br> --- <br> 111AAA <br> [root @ bkjia src] # <br> </span>

The first line of the diff result "4c4" is a prompt to indicate the location of the change. The previous "4" indicates that the first line of f1 has changed; the "c" in the middle indicates that the change mode is content change. Other modes include "add" (a, addition) and "delete" (d, deletion ); the following "4" indicates that the 4th rows of f2 are changed.

The second line "<111aaa" indicates that the row (that is, row 4th) needs to be removed from f1, and the second line "111aaa" indicates the content of the row.

The third line "---" is used to separate f1 and f2.

The fourth line "> 111AAA" is similar to the second line. The preceding greater than sign indicates that f2 adds the row, and the following "111AAA" indicates the content of the row.

The diff in the context format is as follows:

[Root @ bkjia src] # diff-c a.txt B .txt
* ** A.txt 18:21:50. 455124628 + 0800
--- B .txt 18:21:53. 391173006 + 0800
***************
* ** 1, 7 ****
111
Aaa
AAA
! 111aaa
Aaa
AAA
111
--- 1, 7 ----
111
Aaa
AAA
! 111AAA
Aaa
AAA
111
[Root @ bkjia src] #

 

The first part shows the basic information of the two files: File Name and time information. "***" indicates the file before the change, and "---" indicates the file after the change.

Part 2 #15 asterisks separate the basic information of the file from the changed content.

The third part shows the file before the change, that is, f1. In this case, not only the 4th rows that have changed, but also the first three rows and the last three rows are displayed, so a total of seven rows are displayed. Therefore, the preceding "*** 1st ***" indicates that there are seven consecutive rows starting from the first row. In addition, there is a flag at the beginning of each row of the file content. If it is null, the row does not change. If it is an exclamation point (!), Indicates that the row has been changed; if it is a minus sign (-), it indicates that the row is deleted; if it is a plus sign (+), it indicates that the row is added.

The four parts show the changed file, namely f2.

Merge format diff

[Root @ bkjia src] # diff-u a.txt B .txt
--- A.txt 18:21:50. 455124628 + 0800
++ B .txt 18:21:53. 391173006 + 0800
@-+ @@
111
Aaa
AAA
-111aaa
+ 111AAA
Aaa
AAA
111
[Root @ bkjia src] #

The first part is the basic information of the file. "---" Indicates the file before the change, and "++" indicates the file after the change.

In the second part, the change location uses two @ as the beginning and end. The preceding "-1st" is divided into three parts: the minus sign indicates the first file (f1), the "1" indicates rows, and the "7" indicates 7 consecutive rows. When combined, it indicates that the first file contains seven consecutive lines starting from the first row. Similarly, "+ 1st" indicates that after the change, the second file will start with seven consecutive lines starting from.

The third part is the specific content of the change. In addition to the changed rows, three rows are displayed in each context. It combines the contexts of two files and displays them together. Therefore, it is called "merge format ". The front flag of each row. If it is null, it indicates no change. The minus sign indicates the row to be deleted from the first file. the plus sign indicates the row added to the second file.

Side-by-side format output

[Root @ bkjia src] # diff-y-W 50 a.txt B .txt
111 111
Aaa
AAA
111aaa | 111AAA
Aaa
AAA
111 111
[Root @ bkjia src] #

"|" Indicates that the content of the first and second files is different.

"<" Indicates that the subsequent file contains one line less content than the previous file.

">" Indicates that the subsequent file contains one line more content than the previous file.

This article permanently updates the link address:

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.