Patch Command Tutorial With Examples For Linux

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/patch-command-tutorial-with-examples-for-linux/
PINTEREST
PINTEREST
INSTAGRAM

Patch is a command that is used to apply patch files to the files like source code, configuration. Patch files holds the difference between original file and new file. In order to get the difference or patch we use diff tool.

Software is consist of a bunch of source code. The source code is developed by developers and changes in time. Getting whole new file for each change is not a practical and fast way. So distributing only changes is the best way. The changes applied to the old file and than new file or patched file is compiled for new version of software.

Syntax

Help

Help

Help

Create Patch File

Now we will create patch file in this step but we need some simple source code with two different version. We call the source code file name as myapp.c .

myapp_old.c

myapp.c

Now we will create a patch file named myapp.patch .

Create Patch File

Create Patch File

We can print myapp.patch file with following command

LEARN MORE  How To Mount Ext4 With Alternative Super Block In Linux

Apply Patch File

Now we have a patch file and we assume we have transferred this patch file to the system which holds the old source code which is named myapp_old.patch . We will simply apply this patch file. Here is what the patch file contains

  • the name of the patched file
  • the different content

Apply Patch File

Apply Patch File

Take Backup Before Applying Patch

One of the useful feature is taking backups before applying patches. We will use -b option to take backup. In our example we will patch our source code file with myapp.patch .

Take Backup Before Applying Patch

Take Backup Before Applying Patch

The backup name will be the same as source code file just adding the .orig extension. So backup file name will be myapp.c.orig

Set Backup File Version

While taking backup there may be all ready an backup file. So we need to save multiple backup files without overwriting. There is -V option which will set the versioning mechanism of the original file. In this example we will use numbered versioning.

Set Backup File Version

Set Backup File Version

As we can see from screenshot the new backup file is named as number like myapp.c.~1~

Validate Patch File Without  Applying or Dry run

We may want to only validate or see the result of the patching. There is a option for this feature. We will use --dry-run option to only emulate patching process but not change any file really.

LEARN MORE  How Change Run Levels In Linux?

Reverse Patch

Some times we may need to patch in reverse order. So the apply process will be in reverse. We can use -R parameter for this operation. In the example we will patch myapp_old.c rather than myapp.c

Reverse Patch

Reverse Patch

As we can see that new changes are reverted back.

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/patch-command-tutorial-with-examples-for-linux/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)