How to Run Patch Command in Linux?

Learn via video courses
Topics Covered

The patch command is used to insert patch files into text or source code files. It accepts a patch file as input and updates the original files with the differences. To determine the difference, we employ the diff tool. The diff command compares the contents of two files and prints a list of the differences, where diff is short for "differences".

Syntax

The Linux patch command is a powerful tool used in Linux and Unix-like operating systems to apply changes, or patches, to files. It is commonly used for updating software or fixing bugs in source code files.

To run the patch in Linux, you need two things: the original file and the patch file. Here is the basic syntax:

Let's break down the command:

patch: This is the command itself.

[options]: You can specify various options to customize the behavior of the patch command.

<original_file>: This is the path to the original file you want to patch.

<patch_file>: This is the path to the Linux patch file containing the changes.

Options for the Linux patch command:

The Linux patch command provides several options to control its behavior. Here are some commonly used options:

OptionsDescription
-p<n> or --strip=<n>Specifies the number of leading directory components to ignore when applying the patch. This is useful when the directory structure in the patch file differs from the current directory structure.
-i <patch_file> or --input=<patch_file>Specifies the patch file to use. This option is useful if you don't want to provide the patch file as an argument.
-d <directory> or --directory=<directory>Specifies the directory where the original file is located. This option is useful when the patch file does not include the directory information.
-R or --reverseReverses the patch by undoing the changes made to the original file. This option is useful when you want to remove the changes applied by a previous patch.
-N or --forwardForces the patch to be applied even if the original file is missing.

These are just a few of the available options. You can refer to the patch command's manual page (man patch) for more details on the available options and their usage.

Checking the Difference with diff

After running the patch command, we will proceed to check the difference. To check for differences between two files, you can use the diff command. The diff command compares the contents of two files and shows the lines that differ between them. It does not apply any changes but simply displays the differences for your review. For example, to check the differences between two files named file1.txt and file2.txt, you would run the diff command like this:

The output will show the lines that are different between the two files, allowing you to inspect and understand the changes without modifying any of the files. Here is an example using the files discussed in the section above:

Create a patch file called myfile.patch and save it:

By using the following command, you can print the patch file:

checking the difference with diff

So, the patch command is used to apply changes from a patch file to an original file, updating it with the specified modifications. On the other hand, the diff command is used to compare two files and display the differences between them without making any changes. Both commands are valuable tools in software development and system administration tasks.

Application of the Patch File

When applying a patch using the patch in Linux, you need a patch file that describes the differences between the original file and the modified version. Here is a step-by-step guide on how to apply a patch file using the patch command:

  • Make sure you have the original file:

    Ensure that you have the original file to which you want to apply the patch. The patch file contains instructions on how to modify this original file.

  • Obtain the patch file:

    Obtain the patch file that contains the changes or modifications to be applied to the original file. The patch file typically has a .patch extension, but it can also have other extensions such as .diff or .patched.

  • Navigate to the directory:

    Open a terminal and navigate to the directory where the original file is located. This step is necessary because the patch command expects to find the original file in the same location specified within the patch file.

  • Apply the patch:

    Run the patch command, providing the original file and the patch file as arguments.

    The patch command will read the patch file and apply the changes to the original file. For example, Apply a patch using:

    A patch file must be in the directory where the source code file is located.

Before Patching, Make a Backup

Taking a backup before applying a patch is a good practice to ensure you have a copy of the original file in case anything goes wrong during the patching process. Here's a step-by-step guide on how to take a backup before applying a patch using the patch in Linux:

Identify the Original File:

Determine the path and name of the original file you want to patch. For example, let's say the original file is located at /path/to/original/file.txt.

Create a Backup Copy:

Open a terminal and navigate to the directory where the original file is located. Then, create a backup copy of the file using a different name or by adding an extension like .bak to the file. You can use the cp command for this purpose. This command creates a backup copy of file.txt named file.txt.bak in the same directory.

Verify the Backup:

Confirm that the backup copy has been created successfully by listing the files in the directory. You should see both the original file and the backup file listed.

Apply the Patch:

Now you can proceed to apply the patch using the patch command as explained in the previous responses. By having the backup copy, you have a safety net in case the patching process doesn't go as expected.

Verify the Changes:

After applying the patch, review the modified file to ensure that the desired changes have been applied correctly. You can compare it with the backup file to check the differences.

If, for any reason, the patching process introduces unexpected issues or undesired changes, you can restore the original file from the backup copy. You can use the cp command to overwrite the modified file with the backup:

This command replaces the modified file.txt with the backup copy, effectively restoring it to its original state.

In the context of our previous example, we can do this by utilizing the -V switch. Each backup file's version is established by it. Run the following command:

By following these steps and taking a backup before applying a patch, you can minimize the risk of data loss or unintended changes, allowing you to revert to the original file if needed.

Check Patch Files

Validating patch files before applying them using the patch in Linux helps ensure that the patch files are compatible with the original files and can be applied successfully. To validate patch files, you can follow these steps:

After obtaining the original and patch file,

Navigate to the Directory:

Open a terminal and navigate to the directory where the original file is located. This step is important because the patch command expects the original file to be in the same directory specified within the patch file.

Test the Patch:

Use the --dry-run option with the patch command to simulate the patching process without actually modifying any files. This option allows you to validate the patch file before applying it. The basic syntax is as follows:

For example, if your original file is a file. c and the patch file is file.patch, you would run:

The --dry-run option will display the expected changes that would be made to the original file without actually applying them. This step helps you verify that the patch can be successfully applied.

Review the Output:

The patch command, with the --dry-run option, will display output indicating the changes it would make to the original file if the patch were applied. Review the output carefully to ensure that it reflects the desired modifications and that no errors or conflicts are reported.

If the output indicates that the patch would be applied cleanly without errors or conflicts, you can proceed with the actual patching process. If any issues or conflicts are detected, you may need to resolve them before applying the patch.

In the context of our previous example, use the -dry-run option if you want to check or see how patching turns out. The original file is not changed in any way by it:

By following these steps and using the --dry-run option, you can validate patch files before applying them using the patch in Linux. This helps ensure that the patch files are compatible and can be applied successfully, reducing the risk of errors and issues during the actual patching process.

Undo/Reverse a Patch

To reverse or undo a patch that has already been applied, use the -R option.

reverse a patch

In the Linux operating system, the patch command allows us to apply patch files to source codes or configuration files. Software updates require the use of the patch file. The diff program is used to obtain the difference or patch between the original and new files, which are stored in patch files. We reviewed how to use the diff and patch commands with a variety of parameters, including creating backups, conducting dry runs, and reversing the patch that has been applied.

Conclusion

To summarize, here are the key points on how to run the patch in Linux:

  • The patch command is used to apply changes, or patches, to files in Linux and Unix-like systems.
  • It requires an original file and a patch file that contains the differences between the original and modified versions.
  • The basic syntax is: patch [options] <original_file> <patch_file>.
  • Options such as -p, -i, -d, -R, and -N can be used to customize the behavior of the patch command.
  • It is recommended to take a backup of the original file before applying a patch to have a safety net in case of issues.
  • The patch command compares the differences in the patch file and applies them to the original file, effectively updating it.
  • The command output provides information about the applied changes and any conflicts or issues that may have occurred.
  • The patch command should be used after verifying the compatibility of the patch file with the original file using the --dry-run option.
  • If the patch fails to apply cleanly, manual conflict resolution may be required.
  • The patch command is a valuable tool for software development and system administration tasks in Linux.

By following these guidelines, you can effectively use the patch command to apply patches and modify files in Linux.