Detailed explanation of diff commands in linux commands (Comparison of file content)

Source: Internet
Author: User
Tags one more line
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. The following are detailed instructions and examples.

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.

1. command format:
Diff [parameter] [file 1 or directory 1] [File 2 or directory 2]

2. command functions:
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.

3. command parameters:
-Specify the number of lines of text to be displayed. This parameter must be used with the-c or-u parameter.
-A or -- text diff: by default, only text files are compared row by row.
-B or -- ignore-space-change do not check the difference of space characters.
-B or -- ignore-blank-lines do not check blank lines.
-C: display all internal text and mark the differences.
-C or -- context is the same as executing the "-c-" command.
-D or -- minimal uses different algorithms for comparison in smaller units.
-D or ifdef the output format of this parameter can be used for the Frontend Processor.
-E or -- ed the output format of this parameter can be used in the script file of ed.
The format output by-f or-forward-ed is similar to that output by ed script files, but different places are displayed in the order of the original files.
-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.
-L or -- paginate submits the result to the pr program for paging.
-N or -- rcs: The comparison result is displayed in the format of RCS.
-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.
-P: if the comparison file is a C program code file, the function name of the difference is displayed.
-P or -- unidirectional-new-file is similar to-N, but only when the second directory contains files not in the first Directory, to compare this file with a blank file.
-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.
-U,-U, or -- uniied = are combined to display different file content.
-V or -- version displays version information.
-W or -- ignore-all-space ignores all space characters.
-W or -- width specifies the column width when the-y parameter is used.
-X or -- exclude does not compare the files or directories specified in the option.
-X or -- exclude-from you can save the file or directory type as a text file, and then specify this text file in =.
-Y or -- side-by-side displays similarities and differences of files in parallel.
-- Help: displays help.
-- When left-column uses the-y parameter, if the content of a row in the two files is the same, only the content of the row is displayed in the left column.
-- Suppress-common-lines only show differences when using the-y parameter.

4. example:
Instance 1: compare two files

Copy codeThe code is as follows:
[Root @ localhost test3] # diff log2014.log log2013.log
3c3
& Lt; 2014-03
---
& Gt; 2013-03
8c8
<2013-07
---
& Gt; 2013-08
11, 12d10
<2013-11
<2013-12

Note: the above "3c3" and "8c8" indicate that the log2014.log and log20143log files have different content in 3 and 8th rows; "11, 12d10 "indicates that the first file contains 11th and 12 more lines than the second file.
There are three tips for the normal display format of diff:

Copy codeThe code is as follows:
A-add
C-change
D-delete

Instance 2: side-by-side output
Command: diff log2013.log log2014.log-y-W 50
Output:

Copy codeThe code is as follows:
[Root @ localhost test3] # diff log2014.log log2013.log-y-W 50
2013-01 2013-01
2013-02 2013-02
2014-03 | 2013-03
2013-04 2013-04
2013-05 2013-05
2013-06 2013-06
2013-07 2013-07
2013-07 | 2013-08
March 2013-09 2013-09
2013-10 2013-10
2013-11 <
2013-12 <
[Root @ localhost test3] # diff log2013.log log2014.log-y-W 50
2013-01 2013-01
2013-02 2013-02
2013-03 | 2014-03
2013-04 2013-04
2013-05 2013-05
2013-06 2013-06
2013-07 2013-07
2013-08 | 2013-07
March 2013-09 2013-09
2013-10 2013-10
> 2013-11
> 2013-12

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

Instance 3: context output format
Command: diff log2013.log log2014.log-c
Output:

Copy codeThe code is as follows:
[Root @ localhost test3] # diff log2013.log log2014.log-c
* ** Log2013.log 16:36:26. 000000000 + 0800
--- Log2014.log 18:01:54. 000000000 + 0800
***************
* ** 1, 10 ****
2013-01
2013-02
! 2013-03
2013-04
2013-05
2013-06
2013-07
! 2013-08
2013-09
2013-10
--- 1, 12 ----
2013-01
2013-02
! 2014-03
2013-04
2013-05
2013-06
2013-07
! 2013-07
2013-09
2013-10
+ 2013-11
+ 2013-12 [root @ localhost test3] # diff log2014.log log2013.log-c
* ** Log2014.log 18:01:54. 000000000 + 0800
--- Log2013.log 16:36:26. 000000000 + 0800
***************
****
2013-01
2013-02
! 2014-03
2013-04
2013-05
2013-06
2013-07
! 2013-07
2013-09
2013-10
-2013-11
-2013-12
--- 1, 10 ----
2013-01
2013-02
! 2013-03
2013-04
2013-05
2013-06
2013-07
! 2013-08
2013-09
2013-10 [root @ localhost test3] #

Note:
This method provides a description of the comparison file in the first two lines. here there are three special characters:
"+" Compares the latter of a file with one more line
"-" Compares the latter of a file with a row less than the previous one.
"!" The comparison file has different rows

Example 4: output in uniform format
Command: diff log2014.log log2013.log-u
Output:

Copy codeThe code is as follows:
[Root @ localhost test3] # diff log2014.log log2013.log-u
--- Log2014.log 18:01:54. 000000000 + 0800
++ Log2013.log 16:36:26. 000000000 + 0800
@-+ @@
2013-01
2013-02
-2014-03
+ 2013-03
2013-04
2013-05
2013-06
2013-07
-2013-07
+ 2013-08
2013-09
2013-10
-2013-11
-2013-12

Note:
The first part is the basic information of the file:
--- Log2014.log 18:01:54. 000000000 + 0800
++ Log2013.log 16:36:26. 000000000 + 0800
"---" 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 (that is, log2014.log), the "1" indicates rows, and the "12" indicates 12 rows in a row. When combined, it indicates that the first file contains 12 consecutive rows starting from 1st rows. Similarly, "+ 1st" indicates 10 consecutive lines starting from the first row of the second file after the change.

Instance 5: compare different folders
Command: diff test3 test6
Output:

Copy codeThe code is as follows:
[Root @ localhost test] # diff test3 test6
Only in test6: linklog. log
Only in test6: log2012.log
Diff test3/log2013.log test6/log2013.log
1, 10c1, 3
<2013-01
<2013-02
& Lt; 2013-03
<2013-04
<2013-05
<2013-06
<2013-07
<2013-08
<2013-09
<2013-10
---
> Hostnamebaidu = baidu.com
> Hostnamesina = sina.com
> Hostnames = true
Diff test3/log2014.log test6/log2014.log
1, 12d0
<2013-01
<2013-02
& Lt; 2014-03
<2013-04
<2013-05
<2013-06
<2013-07
<2013-07
<2013-09
<2013-10
<2013-11
<2013-12
Only in test6: log2015.log
Only in test6: log2016.log
Only in test6: log2017.log
[Root @ localhost test] #

Example 6: compare two different files and produce patches
Command: diff-ruN log2013.log log2014.log> patch. log
Output:

Copy codeThe code is as follows:
[Root @ localhost test3] # diff-ruN log2013.log log2014.log> patch. log
[Root @ localhost test3] # ll
Total 12
-Rw-r -- 2 root 80 12-07 16:36 log2013.log
-Rw-r -- 1 root 96 12-07 18:01 log2014.log
-Rw-r -- 1 root 248 12-07 21:33 patch. log
[Root @ localhost test3] # cat patc. log
Cat: patc. log: no file or directory
[Root @ localhost test3] # cat patch. log
--- Log2013.log 16:36:26. 000000000 + 0800
++ Log2014.log 18:01:54. 000000000 + 0800
@-+ @@
2013-01
2013-02
-2013-03
+ 2014-03
2013-04
2013-05
2013-06
2013-07
-2013-08
+ 2013-07
2013-09
2013-10
+ 2013-11
+ 2013-12 [root @ localhost test3] #

Instance 7: Patching
Command: cat log2013.log
Output:

Copy codeThe code is as follows:
[Root @ localhost test3] # cat log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10 [root @ localhost test3] # patch log2013.log patch. log
Patching file log2013.log
[Root @ localhost test3] #
[Root @ localhost test3] # cat log2013.log
2013-01
2013-02
2014-03
2013-04
2013-05
2013-06
2013-07
2013-07
2013-09
2013-10
2013-11
2013-12 [root @ localhost test3] #

Related Article

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.