Linux patch Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux patch command to apply and revert changes to files. The patch command is a powerful tool for updating or modifying files by applying differences between files. You will start by understanding the purpose and syntax of the patch command, then learn how to apply a patch to a file, and finally, how to revert a patch. This lab provides practical examples to help you master the use of the patch command in your Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/VersionControlandTextEditorsGroup -.-> linux/diff("`File Comparing`") linux/VersionControlandTextEditorsGroup -.-> linux/patch("`Patch Applying`") subgraph Lab Skills linux/cat -.-> lab-422853{{"`Linux patch Command with Practical Examples`"}} linux/diff -.-> lab-422853{{"`Linux patch Command with Practical Examples`"}} linux/patch -.-> lab-422853{{"`Linux patch Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the patch Command

In this step, you will learn about the purpose and syntax of the patch command in Linux. The patch command is used to apply differences between files to an original file, allowing you to update or modify the file with changes made in a separate file.

First, let's understand the basic syntax of the patch command:

patch [options] [original_file [patch_file]]

The patch command takes the following options:

  • -p<number>: Strips the specified number of leading directories from file names.
  • -i <patch_file>: Specifies the patch file to be applied.
  • -R: Reverses the patch, removing the changes instead of applying them.
  • -r <reject_file>: Specifies the reject file to be used.

Now, let's see an example of using the patch command:

## Create a sample file
echo "This is the original file." > original.txt

## Create a patch file
echo "This is the modified file." > modified.txt
diff -u original.txt modified.txt > patch.diff

## Apply the patch
patch -p0 -i patch.diff

Example output:

patching file original.txt

In this example, we first create a sample file original.txt. Then, we create a modified version of the file modified.txt and generate a patch file patch.diff using the diff command. Finally, we apply the patch using the patch command, which updates the original.txt file with the changes from the patch file.

Apply a Patch to a File

In this step, you will learn how to apply a patch to a file using the patch command.

First, let's create a sample file and a patch file:

## Create a sample file
echo "This is the original file." > original.txt

## Create a modified version of the file
echo "This is the modified file." > modified.txt

## Generate a patch file
diff -u original.txt modified.txt > patch.diff

Now, let's apply the patch to the original file:

## Apply the patch
patch -p0 -i patch.diff

Example output:

patching file original.txt

After applying the patch, the original.txt file should now contain the changes from the modified.txt file.

Let's verify the contents of the original.txt file:

cat original.txt

Example output:

This is the modified file.

As you can see, the original.txt file has been updated with the changes from the patch file.

Revert a Patch

In this step, you will learn how to revert a patch using the patch command.

First, let's create a sample file and a patch file:

## Create a sample file
echo "This is the original file." > original.txt

## Create a modified version of the file
echo "This is the modified file." > modified.txt

## Generate a patch file
diff -u original.txt modified.txt > patch.diff

Now, let's apply the patch to the original file:

## Apply the patch
patch -p0 -i patch.diff

Example output:

patching file original.txt

To revert the patch, we can use the -R option:

## Revert the patch
patch -R -p0 -i patch.diff

Example output:

patching file original.txt

After reverting the patch, the original.txt file should now contain the original contents.

Let's verify the contents of the original.txt file:

cat original.txt

Example output:

This is the original file.

As you can see, the original.txt file has been reverted to its original state.

Summary

In this lab, you learned about the purpose and syntax of the patch command in Linux, which is used to apply differences between files to an original file, allowing you to update or modify the file with changes made in a separate file. You also learned how to apply a patch to a file using the patch command, by first creating a sample file, modifying it, generating a patch file, and then applying the patch to the original file. Finally, you learned how to revert a patch using the -R option of the patch command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like