One Linux command per day (33): diff

Posted by Komodo on Wed, 10 Jul 2019 19:42:14 +0200

Diff is used to find out the different contents of a file. In particular, diff prints out changes to each line on the command line for two different versions of the same name file, and its latest version also supports comparing binary files. The patch program in Linux system can update the file content of a.c to b.c according to the output of diff (patch). Diff is an indispensable part of version control tools such as svn, cvs, git and so on.

1. Command format

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

2. Command Function

Diff is used to compare the contents of a single file or directory: if the comparison is specified as a file, it is valid only when the input is a text file. In a line-by-line fashion, diff compares the similarities and differences of text files; if a directory is specified for comparison, diff compares text files with the same name in both directories. Diff can also be used to list different binary files, common subdirectories, and files that appear only in one directory.

3. Command parameters

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

- a, - text, diff presupposes that only text files are compared line by line.

- b, - ignore-space-change, does not check the difference of space characters.

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

- c. Display all the text and mark the differences.

- c, - context, is the same as executing "- c -"instructions.

- d, - minimal, using different algorithms to compare in smaller units.

- D, ifdef, the output format of this parameter can be used for the pre-processor macro.

- e, - ed, the output format of this parameter can be used in the script file of ED.

- f, -forward-ed, the output format is similar to ED script file, but in the order of the original file to show different places.

- H, - speed-large-files, when larger files, can speed up.

- l, - ignore-matching-lines, do not show the difference between the two files if the two files are different on certain lines that contain both the characters or strings specified in the options.

- I, - ignore-case, do not check the difference between case and case.

- l, - paginate, the results are paginated by the pr program.

- n, - rcs, the comparison results are displayed in RCS format.

- N, - new-file, when comparing directories, if file A only appears in a directory, the Only in directory will be displayed by default; if the - N parameter is used, diff will compare file A with a blank file.

- p, if the comparison file is C language code, then show the difference of function name.

- P, - unidirectional-new-file, similar to - N, will only be compared with a blank file if the second directory contains files that are not in the first directory.

- q, - brief, showing only differences, not detailed information.

- r, - recursive, compare files in subdirectories.

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

- S, - start - file, when comparing directories, compares from the specified file.

- t, - expand-tabs, when output, expand the tab character.

- T, - initial-tab, with tab characters in front of each line for alignment.

- u, - U, - unified=, which shows the difference in file content in a merged way.

- v, - version, showing version information.

- w, - ignore-all-space, ignoring all the space characters.

- W, - width, specify the column width when using the - y parameter.

- x, - exclude, does not compare the files or directories specified in the options.

- X, - exclude-from, saves the file or directory type as a text file, and then specifies the text file in =

- y, - side-by-side, displaying the similarities and differences of files in parallel.

- help, show help.

left-column, when using the-y parameter, if the contents of one line of the two files are the same, the contents of the line are displayed only in the left column.

suppress-common-lines show only differences when using the-y parameter.

4. Use examples

Example 1: Compare two documents

# diff log2014.log log2013.log 

3c3
< 2014-03
---
> 2013-03
8c8
< 2013-07
---
> 2013-08
11,12d10
< 2013-11
< 2013-12

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

diff's normal display format has three prompts: a, a D d; c, change; d, delete

Example 2: Output in side-by-side format (commonly used)

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

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

# 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

Description: "|" means that the contents of the two files are different, "<" means that the latter file has one line less than the former one, and ">" means that the latter file has one line more than the former one (contrary to the normal larger than less than the symbol).

Example 3: Context output format

Command: diff log2013. log log2014. log-c

# 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

# 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

Explanation: This method is illustrated in the first two lines of the comparative document. There are three special characters in it: "+" means that the latter is one more line than the former; "-" means that the latter is one less line than the former; "!" It shows that there are differences between the two at the same number.

Example 4: Uniform format output (commonly used)

Command: diff log2014. log log2013. log-u

# 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

The first part is the basic information of the document, - log2014.log 2012-12-07 18:01:54.000000000+0800, +++ log2013.log 2012-12-07 16:36:26.000000000+0800, where "-" means the document before the change, and "++" means the document after the change.

In the second part, two @ are used as the beginning and the end of the change. For @ 1, 12 + 1, 10 @, "-1, 12" is divided into three parts: minus sign means the first file (log2014.log), ""1"means the first line,""12" means 12 consecutive lines. Together, it means 12 consecutive lines of the first file from the first line. Similarly, "+1,10" means that after the change, the second file starts at line 1 for 10 consecutive lines.

Here are three special characters: "+" means the latter is one more line than the former; "-" means the latter is one less line than the former.

Example 5: Compare different folders

Command: diff test3 TEST6

# 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


Example 6: Compare the differences between the two files and generate patches (commonly used)

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

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

# 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

# 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-07
-2013-08
 2013-09
 2013-10
+2013-11
+2013-12

Note: Where "-" means the document before the change, and "++" means the document after the change.

Example 7: Patching (commonly used)

Command: patch log2013.log patch.log
Description: Compare the difference between the two files, insert the difference content into the same location of the target file, and cover the original content.

# cat log2013.log

2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10

# patch log2013.log patch.log 

patching file log2013.log

# 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

Topics: less Linux svn git