Easy Tutorial
❮ Linux Comm Mkbootdisk Linux Comm Ftp ❯

Linux patch Command

Linux Command Manual

The Linux patch command is used to apply patches to files.

The patch command allows users to modify and update original files using patch files. If only one file is to be patched, commands can be executed sequentially directly in the command line. If patch files are used, multiple files can be patched at once, which is also one of the methods for upgrading the Linux kernel.

Syntax

patch [-bceEflnNRstTuvZ][-B &lt;backup prefix string>][-d &lt;working directory>][-D <symbol>][-F &lt;fuzz factor>][-g &lt;control value>][-i &lt;patch file>][-o &lt;output file>][-p &lt;strip level>][-r &lt;reject file>][-V &lt;backup method>][-Y &lt;backup prefix string>][-z &lt;backup suffix string>][--backup-if-mismatch][--binary][--help][--nobackup-if-mismatch][--verbose][original file &lt;patch file>] or patch [-p &lt;strip level>] < [patch file]

Parameters:

Example

Use the patch command to upgrade the file "testfile1" with the upgrade patch file "testfile.patch" by entering the following command:

$ patch -p0 testfile1 testfile.patch    # Upgrade the file using the patch program

Before using this command, you can view the contents of "testfile1" using the "cat" command. To generate a patch file by comparing the file to be modified with the original file, use the "diff" command. The specific steps are as follows:

$ cat testfile1                 # View the contents of testfile1  
Hello, This is the first file!  
$ cat testfile2                 # View the contents of testfile2  
Hello, This is the second file!  
$ diff testfile1 testfile2          # Compare the two files  
1c1  
< Hello, This is the first file!  
---  
> Hello, This is the second file!  
# Save the comparison result to the testfile.patch file  
$ diff testfile1 testfile2 > testfile.patch     
$ cat testfile.patch                # View the contents of the patch file  
1c1  
< Hello, This is the first file!  
---  
> Hello, This is the second file!  
# Use the patch file to upgrade testfile1  
$ patch -p0 testfile1 testfile.patch      
patching file testfile1  
$ cat testfile1                  # View the contents of testfile1 again  
# The testfile1 file is modified to have the same content as testfile2  
Hello, This is the second file!

Note: In the command code above, "$ diff testfile1 testfile2 > testfile.patch" uses the operator ">" to write the data from the file on the left to the file on the right. Here, it means writing the result of the comparison between the two files into the "testfile.patch" file.

Linux Command Manual

❮ Linux Comm Mkbootdisk Linux Comm Ftp ❯