Linux patch tool patch AND diff
I. Tool Overview:
Diff patch Generation Tool
Diff can be followed by two file names or two directory names to generate Patches
Patch patching Tool
Patch purpose: generate the target file based on the original file and Patch file
In mathematics, diff is similar to the Difference Operation on two sets. patch is similar to the sum operation on two sets. Diff compares the differences between two files or file sets and records them to generate a diff file, which is also a patch file we often call patch files. Patch can apply the diff file to one of the original two sets to obtain another set.
For example, if file A and file B generate patch file C after diff, the process is equivalent to | A-B | = C, the patch process is B + C = A or A-C = B (A + C = B or B-C = ). Therefore, as long as we can get any two of the three files A, B, and C, we can use the diff and patch tools to generate another file. This is the beauty of diff and patch.
Patch was invented by Larry Wall, a talented programmer and inventor of Perl. It should efficiently communicate with the needs of program source code. With the development of source code running represented by Linux, the concept of patch has become part of the collective unconscious of open source code initiators, contributors, and participants. Patch only contains the modified source code, which is of great significance for the collaborative development mode of the open source community, it means that the release of new software versions and defects or improvements to software can be released with smaller files, which can reduce the amount of network transmission and facilitate the management of software maintainers.
Patch files are in multiple formats. Different formats are supported on different platforms. However, the most common formats are context and unified. The context format is widely used and is the de facto standard of the patch file format. The format contains the difference and several adjacent lines. The adjacent lines are called contexts. Although these lines do not change, they appear in the patch file, which makes the restored patch program more fault tolerant. The uniied format is common in the GNU patch implementation. This format is used for linux kernels released in the form of patches.
In addition, there are other less-used formats, such as the Normal format, side-by-side comparison mode (side-by-side), ed script and RCS script Mode.
. In addition to the side-by-side comparison mode, you can easily observe file differences. Most of the other formats are to be compatible with the old patch format.
Ii. Usage of tools
1. diff usage
Diff can be followed by two file names or two directory names to generate a patch. For example:
Diff [option] oldfile newfile
If it is a directory name and a file name, it only applies to files with the same name under that directory. For example:
Diff/usr/xu mine
Compare the files named mine in the/usr/xu directory with the mine files in the current directory.
Common option options of Diff include:
L-r performs recursive comparison when comparing directories to generate patches for the entire code tree.
L-u output is in a unified format. diff has two formats: "traditional" and "unified". Currently, the "unified" format is generally used. In comparison, the file generated in a unified format is large, but it contains more information, which is helpful for reading and positioning.
L-N indicates that if the file does not exist, it is equivalent to an empty file. This is used to generate a patch for adding or deleting files.
L-a patch contains binary files which are missing and printed to the standard output by diff. Therefore, it is usually redirected to a file and suffixed with a patch, which is also called a patch file. Binary File Description: binary files can be stored in the patch file in the original way. Diff can be generated (added with the-a option), and patch can also be identified. If you think such a patch file is too ugly, one of the solutions is to use uencode to process the binary file.
If there are two directories, they act on all the files under the directory without recursion. If we want Recursive Execution, we need to use the-r parameter. The diff file format generated without adding any parameters is a simple format, which only marks different lines and content. We need a more detailed format to identify different context environments, which is more conducive to improving the recognition capability of patch commands. In this case, you can use the-c switch. For more information, see table 1 diff command line options and parameters.
Diff command line options and parameters:
-A reads all files as text, makes the files look like binary, and compares them row by row. This parameter must be added to the binary file]
-B. Ignore the change in the number of white spaces in the block.
-B. Ignore the changes caused by inserting or deleting empty rows.
-C generates output in the context format
-C [num] generates "context" (context) format output, displaying the content of the num rows before and after the block. If the num value is not specified, the content of the three rows before and after the block is displayed.
-H modify the diff method for processing large files
-I case-insensitive
-I regexp ignore inserting or deleting rows that match the regular expression regexp
-L add the page number of the output result through the pr command
-P: displays the Block C function.
-Q: only whether the report files are different; no output difference
-R performs recursive comparison when comparing directories to generate patches for the entire code tree.
-S reports that two files are the same (the default behavior is not to report the same files)
-T: The tab is expanded to blank when output.
-U generates "unified" output
-U [num] generates "unified" (uniied) output, displaying the content of the num rows before and after the block. If the num value is not specified, the content of the three rows before and after the block is displayed.
-V: print the diff version number.
-Ignore blank space when comparing rows by row
-W cols: If a side-by-side output is generated (see-y), each column of the output has cols character width.
-X pattern when comparing directories, ignore any files and subdirectories that match pattern.
-Y generates output in a side-by-side format
-N indicates that if the file does not exist, it is equivalent to an empty file, which is used to generate a patch for adding or deleting files.
For more details, please continue to read the highlights on the next page:
Use diff and patch production and patching in Linux
Linux diff command parameters and usage --- comparison of file differences