[Linux] Linux diff -- Compare two files and output the differences

Reprinted: https://blog.csdn.net/mosesmo1989/article/details/51093631

The diff command is a very important tool on linux for comparing the contents of files, especially comparing two files with different versions to find the changes. diff prints the changes for each line on the command line. The latest version of diff also supports binary files. The output of the diff program is called a patch, because there is also a patch program in the Linux system, which can update the file content of ac to bc according to the output of diff. diff is an integral part of version control tools like svn, cvs, git, etc.

1. Command format:

diff[parameter][file1 or directory1][file2 or directory2]

2. Command function:

The diff command can compare the contents of individual files or directories. If files are specified to be compared, this will only work if the input is a text file. On a line-by-line basis, compare text files for similarities and differences. If you specify to compare directories, the diff command will compare text files with the same name in two directories. List different binaries, common subdirectories, and files that only appear in one directory.

3. Command parameters:

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

 The -a or --text diff preset will only compare text files line by line.

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

-B or --ignore-blank-lines Do not check for blank lines.

-c Display the entire text, and mark the differences.

-C or --context is the same as executing the "-c-" command.

-d or --minimal use a different algorithm to compare in smaller units.

-D or ifdef The output format of this parameter can be used with preprocessor macros.

-e or --ed The output format of this parameter can be used for ed script files.

The output format of -f or -forward-ed is similar to the script file of ed, but the difference is displayed in the order of the original file.

-H or --speed-large-files Speed ​​up when comparing large files.

-l or --ignore-matching-lines If the two files differ on several lines that both contain the characters or strings specified in the option, do not display the differences between the two files.

-i or --ignore-case do not check case differences.

-l or --paginate pass the results to the pr program for pagination.

-n or --rcs will show the comparison results in RCS format.

-N or --new-file When comparing directories, if file A only appears in a certain directory, the default will display: Onlyin directory: If the -N parameter is used for file A, diff will compare file A with a blank File comparison.

-p If the compared file is a C language program code file, display the function name where the difference exists.

-P or --unidirectional-new-file Like -N, but only if the second directory contains a file that the first directory does not have, the file will be compared to an empty file.

-q or --brief only show differences, not detailed information.

-r or --recursive Compare files in subdirectories.

-s or --report-identical-files If no differences are found, still display information.

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

-t or --expand-tabs Expand tab characters on output.

-T or --initial-tab Prepend the tab character to each line for alignment.

-u, -U or --unified= Display differences in file contents in a merged manner.

-v or --version Display version information.

-w or --ignore-all-space ignore all whitespace characters.

-W or --width When using the -y parameter, specify the column width.

-x or --exclude Do not compare files or directories specified in options.

-X or --exclude-from You can save the file or directory type as a text file, then specify this text file in =.

-y or --side-by-side Display similarities and differences in files side by side.

--help Display help.

--left-column When using the -y parameter, if the content of a line in the two files is the same, only the content of the line will be displayed in the left column.

--suppress-common-lines Only show the difference when using the -y parameter.

4. Example of use:

Example 1: Comparing two files

Order:

output:

[root@localhost test3]#diff log2014.log log2013.log  

3c3

< 2014-03

---

> 2013-03

8c8

< 2013-07

---

> 2013-08

11,12d10

< 2013-11

< 2013-12

illustrate:

The above "3c3" and "8c8" indicate that the log2014.log and log20143log files have different contents in line 3 and line 8; "11,12d10" indicates that the first file has more lines 11 and 12 than the second file .

The normal display format of diff has three hints:

a - add

c - change

d - delete

Example 2: Side-by-side format output

Order:

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                 2013-04

2013-05                 2013-05

2013-06                 2013-06

2013-07                 2013-07

2013-07               | 2013-08

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

2013-09                 2013-09

2013-10                 2013-10

                      > 2013-11

                      > 2013-12

illustrate:

"|" means that the contents of the two files before and after are different

" < " means that the following file is one line less than the previous file

" > " means that the following file has one more line of content than the previous file

Example 3: Contextual output format

Order:

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 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]#

illustrate:

This method describes the comparison file in the first two lines. There are three special characters:

The latter of the files compared by "+" has one more line than the former

"-" compares the file with one line less than the previous one

"!" compares the lines with differences between the two files

Example 4: Unified format output

Order:

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

+++ 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

illustrate:

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

---log2014.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, "+++" indicates the file after the change.

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

   @@ -1,12 +1,10 @@

The preceding "-1,12" is divided into three parts: the minus sign means the first file (ie log2014.log), "1" means the first line, and "12" means 12 consecutive lines. Taken together, it means that the following are 12 consecutive lines starting from line 1 of the first file. Similarly, "+1,10" means that after the change, it will become 10 consecutive lines starting from line 1 of the second file.

Example 5: Comparing different folders

Order:

diff  test3 test6

output:

[root@localhosttest]# diff test3 test6

Onlyin test6: linklog.log

Onlyin test6: log2012.log

difftest3/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

difftest3/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

Onlyin test6: log2015.log

Onlyin test6: log2016.log

Onlyin test6: log2017.log

[root@localhosttest]#

illustrate:

Example 6: Compare two files for differences and produce a patch

Order:

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:  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]#

illustrate:

Example 7: Patching

Order:

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]#

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906780&siteId=291194637