The diff command is a set of hit values used to compare objects. Other related Commands include CMP, wdiff3, and sdiff.
1. the diff command compares two different files or two files with the same name in different directories.
Diff [Options] srcfile dstfile
The comparison behavior and format of diff are empty by options. Diff compares one row between two files.
Diff can generate several output formats:
Regular normal default format
Context
Same unified
Side by side
1) The regular block format is as follows:
Change_command
<Srcfile line
> Dstfile line
The format of change_command is as follows: first, it is a line number from srcfile or a range of line numbers separated by commas, and then a command character, next is a line number from destfile or a range of line numbers separated by commas.
A-add
B-delete
C-change
2) side-by-side output format
It displays the content of srcfile and dstfile on the screen side by side.
Diff-y
-W: Specify the width of the output Column
>: Indicates that the row is in dstfile but not in srcfile.
<: Indicates that the row is in srcfile instead of dstfile.
|: Mark rows with different files
3) Context output format
The active text generated by diff or the unified block format is an indefinite format.
Diff-C or-C [num]
-C. display the content of the last three rows of the modified row at the same time for each row with a difference
-C [num]: displays the content of the upper and lower num rows of the modified row.
The format of the context block is as follows:
* ** Srcfile srcfile_timestamp
--- Dstfile dstfile_timestamp
************************
* ** Srcfile_line_range ***
Srcfile line
Srcfile line
--- Dstfile_line_range ---
Dstfile line
Dstfile line
The first two lines indicate the file to be compared.
The third line separates the identification information from other output content.
The rest of the output consists of one or more blocks. Each block identifies a file with a three-line context. The upper and lower lines start with two spaces, while the difference lines start with a symbol indicating the difference type, followed by a space.
+ Add a row to srcfile to create dstfile
-Delete a row from srcfile to create dstfile
! Change a row in srcfile to create dstfile
4) unified format
A unified format is a version that modifies the context format. It does not display duplicate contexts and uses other methods to compress the output content.
--- Srcfile srcfile_timestamp
@ Srcfile_range desfile_range @@
Line_from_either_file
Line_from_either_file...
No row starting with @ indicates the start of a block. In a block, the upper and lower lines start with a space, and the difference is that the rows start with + or-to add or delete a row relative to srcfile. You can use-u to change the number of context rows.
2. Compare binary files
Pass the binary file name as a parameter to diff
Diff Hello howdydif
Binary files hello and howdy differ
To display rows with different values, use the-A option.
Diff-a hello Howdy> diffs
To check whether two text files are different but do not show the differences, you can use the-Q option.
Diff-Q hello. c Howdy. c
Files hello. C and howdy. c differ
Diff-u-I include hello. c Howdy. c
Use-I Regexp to ignore the insert or delete rows that match the regular expression Regexp.
-Option B allows diff to ignore the changes in the number of blank lines in the input file.-B allows diff to ignore the changes to delete or insert null rows.
-W ignores the blank changes during row-by-row comparison.
-B ignores the changes in the number of white spaces between input files.
-W: Ignore adding a blank space where no space exists.
2. Understand the diff3 command
Diff3 compares two sets of modifications made by two people, creates 3rd files, saves the merged output results, and points out the conflicts between the two parties.
Diff3 [Options] myfile oldfile Yourfile
If some rows are different in three input files, the hunk containing these rows is called a 3-way hunk.
Use 2-way hunk to indicate rows with only differences between the two files
3-way hunk is identified by "=", while 2-way hunk adds 1, 2, or 3 after "=" to indicate the files that have caused the difference.
PrerequisitesSource codePatch
1. Create a patch
When using diff to create a patch, you must specify the output format as context or unified diff on the command line, and input the file name in the order of the old file prior to the new file in the diff command line, the suffix of the output file name should be. diff or. patch
For example, when a patch is generated based on sigrot.1 and sigrot.2, you can use the following command line to claim a diff in the context format:
Diff-C sigrot.1 sigrout.2> sigrot. Patch
Diff-u sigrot.1 sigrout.1 sigrout.2> sigrout. Patch
Add the-r option when traversing the directory tree.
2 Application Patches
Patch-P0 <sigrot. Patch
-The pnum option specifies the number of "/" to be stripped from the file name contained in the patch before the patch is used. For example, if the file name in the patch is/home/kwall/src/sigrot/sigrot.1, the result of-P1 is home/kwall/src/sigrol/sigrot.1.
-The P4 result is sigrout/sigrot.1.
-P removes all parts except the final file name.
If an error is found after the patch is installed, simply add the-r option to the original command line and install the patch again to obtain the original file.
Patch-P0-r <sigrot. Patch