Detailed explanation of Linux diff command

Time:2021-10-20

The diff (difference) command is often used to compare the contents of two files. The diff command compares the differences between two files in the simplest case. If you use “-” instead of the file parameter, the content to be compared will come from standard input. Next, Liang Xu Xiaobian willLinux diff commandIn detail, I hope it will be helpful to you.

Linux命令

The diff command compares the similarities and differences of text files line by line. If the command specifies to compare directories, files with the same file name in the directory will be compared without any comparison of their subdirectory files.

The diff command has two basic formats, as follows:

  1. Comparison file

[ [email protected] ~]#Diff [options] filename 1 filename 2

  1. Compare directories.

[ [email protected] ~]#Diff [options] directory 1 directory 2

The common options and functions of diff command are shown in Table 1.

Options Function
-Number of rows Specifies how many lines of text to display. This parameter must be used with the – C or – U parameter
-a Diff only compares text files line by line
-b Do not check for differences in space characters
-B Do not check blank lines
-c Show all the text and mark the differences
-C number of rows Same as executing “- C-number of lines” instruction
-d Use different algorithms to compare in smaller units
-D The output format of this parameter can be used for preprocessor macros
-e The output format of this parameter can be used for the script file of ED
-f The output format is similar to the script file of ED, but the differences are displayed in the order of the original file
-H Faster when comparing large files
-I If two files are different in some lines, and these lines contain the characters or strings specified in the options at the same time, the difference between the two files will not be displayed
-i Do not check case differences
-l The results are paged by the PR program
-n Display the comparison results in RCS format
-N When comparing directories, if file a only appears in a directory, the default display will be: only in directory: File A. if the – n parameter is used, diff will compare file a with an empty file
-p If the file to be compared is a program code file of C language, the function name where the difference lies is displayed
-P Similar to – N, but only when the second directory contains a file that the first directory does not have, will this file be compared with a blank file
-q Only differences are displayed, and detailed information is not displayed
-r Compare files in subdirectories
-s If no difference is found, the message is still displayed
-S Compare directories from the specified file
-t When outputting, expand the tab character
-T Precede each line with the tab character for alignment
-u Display the different contents of the file in a merged manner
-v display version information
-w Ignore all space characters
-W When using the – Y parameter, specify the column width
-x Does not compare the files or directories specified in the options
-X You can save the file or directory type as a text file, and then? Specify this text file in
-y Show the similarities and differences of files side by side
–help Show help
–left-column When the – Y parameter is used, if the contents of a line of the two files are the same, the contents of the line are displayed only in the left column

The diff command displays the results in the following two ways:

  • < indicates the data line in the first file.
  • >Represents the data row in the second file.

Note that the diff command can analyze and output different lines of two files. Diff’s output results show what operations need to be done on a file before it can match with the second file (what changes have taken place in the second file compared with the first file), including three operations: a = add, C = change and d = delete. The diff command does not change the contents of the file, but diff can output an ED script to apply these changes.

[example 1] compare the differences between the two files.
Create two sample files named text and text1, and enter the following contents respectively:

//test
11
22

33
44
55

66
77
//test1
11
22
aa
33
44
55
bb
66

Enter the following command in the terminal page:

[[email protected] ~]# diff test test1
3c3
<

> aa
7c7
<

> bb
9,10d8
< 77
<

The meanings of output information are:

  • “3C3”: indicates that the third line of the first file needs to be modified to match the third line of the second file;
  • “—“: indicates the separation line;
  • “> AA”: > indicates the second file. The content to be added in the first file is AA, which is stored in the second file;
  • “> BB”: > indicates the second file. The content to be added in the first file is BB, which is stored in the second file;
  • “9, 10d8”: it means that the 9th and 10th lines in the first file can be deleted to match the 8th line in the second file;
  • “< 77”: indicates that the content to be deleted in the first file is 77.

The above isLiangxu tutorial networkExplain the Linux diff command shared by all friends.

This article is composed of blog one article multi posting platformOpenWriterelease!

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]