Welcome to github.com/hsfxuebao/j… , hope to help you, if you feel ok, please click on the Star

The diff command is an important tool on Linux for comparing the contents of files, especially files of two different versions, to find changes. Diff prints each line change on the command line. The latest version of diff also supports binaries. The output of the diff program is called a patch, because there is a patch program in Linux that updates the contents of the A.C file to b.C based on the output of the DIff program. Diff is an integral 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. Run the following command:

The diff command compares the contents of individual files or directories. If you specify a file to compare, it is valid only if the input is a text file. Compare the similarities and differences of text files in a line-by-line manner. The diff command compares text files with the same name in both directories. Lists different binaries, common subdirectories, and files that only appear in one directory.

3. Command parameters:

– Specifies how many lines of text to display. This parameter must be used with the -c or -u parameter.

-A or –text diff by default only compares text files line by line.

-b or –ignore-space-change does not check for differences in space characters.

-b or –ignore-blank-lines does not check blank lines.

-c Displays all contents and marks the differences.

-c or –context is the same as executing the “-c-” instruction.

-D or –minimal uses a different algorithm and is compared in smaller units.

-d or ifdef The output format of this parameter can be used for preprocessor macros.

-e or — Ed The output format of this parameter can be used in Ed script files.

The format of -for -forward-ed output is similar to that of Ed’s script file, but the differences are displayed in the order of the original files.

-h or –speed-large-files Speeds up large files.

-l or –ignore-matching-lines If two files differ on lines that contain the characters or strings specified in the options, the differences between the two files are not displayed.

-i or –ignore-case does not check for case differences.

-l or –paginate turns the results over to the PR program for pagination.

-n or — RCS displays the comparison results in RCS format.

-n or –new-file When comparing directories, if file A Only exists in A certain directory, the following information is displayed by default: Only in Directory: File A If the -n parameter is used, diff compares file A with A blank file.

-p Displays the function name of the difference if the comparison file is a PROGRAM code file in THE C language.

-p or –unidirectional-new-file is 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 Displays only the difference and no detailed information.

-r or –recursive compares files in subdirectories.

-s or –report-identical-files displays information if no difference is found.

-s or –starting-file When comparing directories, start the comparison from the specified file.

-t or –expand-tabs Expands the TAB character on output.

-t or –initial-tab prefixes each line with a TAB character for alignment.

-u, -u, or –unified= shows differences in file contents by merging them.

-v or –version Displays version information.

-w or –ignore-all-space ignores all space characters.

-w or –width specifies the column width when using the -y argument.

-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 the text file in =.

-y or –side-by-side shows the similarities and differences of files in a side-by-side manner.

–help Displays help.

–left-column When the -y parameter is used, if the contents of a row in two files are the same, the contents of that row are displayed only in the left column.

–suppress-common-lines Displays only the differences when using the -y argument.

4. Example:

Example 1: Compare two files

Command:

Output:

[root@localhost test3]# diff log2014.log log2013.log 3c3 < 2014-03 -- > 2013-03 8c8 < 2013-07 -- > 2013-08 11,12d10 < The 2013-11 < 2013-12Copy the code

Description:

The “3c3” and “8c8″ above indicate that the log2014.log and log20143log files are different in line 3 and line 8; 11,12d10” means the first file has 11 and 12 more lines than the second file.

The diff normal display format has three hints:

a – add

c – change

d – delete

Example 2: Side-by-side formatted output

Command:

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

Output:

[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 The 2013-04, 2013-05, 2013-05, 2013-06, 2013-06, 2013-07 2013-07 2013-07 | 2013-08, 2013-09 2013-09 2013-10 in 2013-10 in 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-09 2013 2013-09 2013-10 2013-10 > 2013-11 > 2013-12Copy the code

Description:

“|” said before and after the two files have different content

< indicates that the following file is 1 line less than the previous file

> indicates that the following file has one more line than the previous file

Example 3: Context output format

Command:

diff log2013.log log2014.log -c

Output:

[root@localhost test3]# diff log2013.log log2014.log -c *** log2013.log 2012-12-07 16:36:26.000000000 +0800 -- Log2014. Log 18:01:54 2012-12-07. 000000000 + 0800 * * * * * * * * * * * * * * * * * * 1, * * * * 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 The 2012-12-07 18:01:54. 000000000 + 0800 - log2013. Log 16:36:26. 2012-12-07 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]#Copy the code

Description:

This way the comparison file is explained in the first two lines with three special characters:

“+” compares the latter file one line more than the preceding one

“-” compares the latter of the file than the previous line

“!” There is a difference between the two lines of the file being compared

Example 4: Uniform format output

Command:

diff log2014.log log2013.log -u

Output:

[root@localhost test3]# diff log2014.log log2013.log -u -- log2014.log 2012-12-07 18:01:54.000000000 +0800 +++ 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-07 +2013-08 2013-09 2013-10-2013-11-12Copy the code

Description:

The first part of it is the basic information of the file:

Log 2012-12-07 18:01:54.000000000 +0800

+++ log2013.log 2012-12-07 16:36:26.000000000 +0800

“– “indicates the file before the change, and “+++” indicates the file after the change.

In the second part, the changing position starts and ends with two @ signs.

@ @ 1 + 1, 10, 12 @ @

The preceding “-1,12” is divided into three parts: the minus sign indicates the first file (log2014.log), “1” indicates line 1, and “12” indicates 12 consecutive lines. Taken together, this means the following 12 consecutive lines from line 1 of the first file. Similarly, “+1,10” means 10 consecutive lines from line 1 in the second file after the change.

Example 5: Compare folders

Command:

diff test3 test6

Output:

[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 < 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 < 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]#Copy the code

Description:

Example 6: Compare two files and produce a patch

Command:

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

Output:

[root@localhost test3]# diff -run log2013.log log2014.log >patch.log [root@localhost test3]# ll total 12-rw-r --r-- 2 root root 80 12-07 16:36 log2013.log -rw-r--r-- 1 root root 96 12-07 18:01 log2014.log -rw-r--r-- 1 root root 248 12-07 21:33  patch.log [root@localhost test3]# cat patc.log cat: patc.log: There is no such file or directory [root@localhost test3]# cat patch.log -- log2013.log 2012-12-07 16:36:26.000000000 +0800 +++ log2014.log 2012-12-07 18:01:54.000000000 +0800 @@ -1,10 +1,12 @@ 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]#Copy the code

Description:

Example 7: Patch installation

Command:

Output:

[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]#
Copy the code

From:

One Linux command per day (36) : diff command