Common Linux commands (28th)-diff

Source: Internet
Author: User
Tags one more line

Common Linux commands (28th)-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.

1.Command Format:

Diff [parameter] [file 1 or directory 1] [file 2 or directory 2]

 

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

 

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

Instance 1:Compare two files

Command: diff log2014.log log2013.log

 

[root@localhost test3]# diff log2014.log log2013.log 3c3< 2014-03---> 2013-038c8< 2013-07---> 2013-0811,12d10< 2013-11< 2013-12
The above "3c3" and "8c8" indicate that the log2014.log and log20143log files have different contents 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:

A-add

C-change

D-delete

 

Example 2:Side-by-side format output

Command: diff log2013.log log2014.log-y-W 50

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.

 

[root@localhost test3]# diff log2014.log log2013.log  -y -W 502013-01                 2013-012013-02                 2013-022014-03               | 2013-032013-04                 2013-042013-05                 2013-052013-06                 2013-062013-07                 2013-072013-07               | 2013-082013-09                 2013-092013-10                 2013-102013-11               <2013-12               <[root@localhost test3]# diff log2013.log log2014.log  -y -W 502013-01                 2013-012013-02                 2013-022013-03               | 2014-032013-04                 2013-042013-05                 2013-052013-06                 2013-062013-07                 2013-072013-08               | 2013-072013-09                 2013-092013-10                 2013-10                      > 2013-11                      > 2013-12

 

Example 3:Context output format

Command: diff log2013.log log2014.log-c

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

 

[root@localhost test3]# diff log2013.log log2014.log  -c*** log2013.log 2012-12-07 16:36:26.000000000 +0800--- log2014.log 2012-12-07 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 2012-12-07 18:01:54.000000000 +0800--- log2013.log 2012-12-07 16:36:26.000000000 +0800****************** 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--- 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]#

 

Example 4:Unified format output

Command: diff log2014.log log2013.log-u

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.

 

[root@localhost test3]# diff log2014.log log2013.log  -u--- log2014.log 2012-12-07 18:01:54.000000000 +0800+++ log2013.log 2012-12-07 16:36:26.000000000 +0800@@ -1,12 +1,10 @@ 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

 

Example 5:Different commands for comparing folders:

Diff test3 test6

 

[root@localhost test]# diff test3 test6Only in test6: linklog.logOnly in test6: log2012.logdiff test3/log2013.log test6/log2013.log1,10c1,3< 2013-01< 2013-02< 2013-03< 2013-04< 2013-05< 2013-06< 2013-07< 2013-08< 2013-09< 2013-10---> hostnamebaidu=baidu.com> hostnamesina=sina.com> hostnames=truediff test3/log2014.log test6/log2014.log1,12d0< 2013-01< 2013-02< 2014-03< 2013-04< 2013-05< 2013-06< 2013-07< 2013-07< 2013-09< 2013-10< 2013-11< 2013-12Only in test6: log2015.logOnly in test6: log2016.logOnly in test6: log2017.log[root@localhost test]# 

 

Example 6:Compare two files and produce Patches

Command: diff-ruN log2013.log log2014.log> patch. log

 

[Root @ localhost test3] # diff-ruN log2013.log log2014.log> patch. log [root @ localhost test3] # ll total 12-rw-r -- r -- 2 root 80 12-07 log2013.log-rw-r -- 1 root 96 12-07 log2014.log- rw-r -- 1 root 248 12-07 21:33 patch. log [root @ localhost test3] # cat patc. logcat: 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 + 2013 @ 2013-01 2014-02-2013-03 + 2013-03 2013-04 2013-05 2013-06-07- 08 + 2013-07 2013-09 2013-10 + 2013-11 + 2013-12 [root @ localhost test3] #

 

Example 7:Patch

Command: patch log2013.log patch. log

[root@localhost test3]# cat log2013.log2013-012013-022013-032013-042013-052013-062013-072013-082013-092013-10[root@localhost test3]# patch log2013.log patch.log patching file log2013.log[root@localhost test3]# [root@localhost test3]# cat log2013.log 2013-012013-022014-032013-042013-052013-062013-072013-072013-092013-102013-112013-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.