What is patch file in linux

7 Patch Command Examples to Apply Diff Patch Files in Linux

When there is a security fix available for a particular software, we typically do a binary upgrade using the package management tools like yum or apt-get. But, there might be situation where you have installed a software by compiling it from the source code. In those situation, how do you apply the security fix to the software? The answer is to download the security patch and apply it to the original source code and re-compile the software. This tutorial explains how to create a patch file using diff, and apply it using patch command.
A patch file is a text file which contains the differences between two versions of the same file (or same source-tree). Patch file is created by using diff command.

1. Create a Patch File using diff

#include int main(int argc, char *argv[])

$ diff -u hello.c hello_new.c > hello.patch

— hello.c 2014-10-07 18:17:49.000000000 +0530 +++ hello_new.c 2014-10-07 18:17:54.000000000 +0530 @@ -1,5 +1,6 @@ #include -int main()

2. Apply Patch File using Patch Command

The “patch” command takes a patch file as input and apply the differences to one or more original file(s), producing patched versions.

The hello.patch file contains the name of the file to be patched. Once the file is patched, both hello.c and hello_new.c will have the content.

3. Create a Patch From a Source Tree

The above example was so simple that it works only with one file. We will see how to create and apply patch for a complete source tree by taking “openvpn” source code as example. I’ve downloaded 2 version of openvpn, openvpn-2.3.2 and openvpn-2.3.4.

tar -xvzf openvpn-2.3.2.tar.gz tar -xvzf openvpn-2.3.4.tar.gz
diff -Naur /usr/src/openvpn-2.3.2 /usr/src/openvpn-2.3.4 > openvpn.patch

The above command will operate recursively and find the differences, and place those differences in the patch file.

Читайте также:  Linux add user with root permissions

4. Apply Patch File to a Source Code Tree

Please note that we are executing the command from /usr/src/. The patch file contains all the filenames in absolute path format( from root ). So when we execute from /usr/src, without the “-p” option, it will not work properly. -p3 tells the patch command to skip 3 leading slashes from the filenames present in the patch file. In our case, the filename in patch file is “/usr/src/openvpn-2.3.2/aclocal.m4”, since you have given “-p3”, 3 leading slashes, i.e. until /usr/src/ is ignored.

5. Take a Backup before Applying the Patch using -b

You can take a backup of the original file before applying the patch command using the -b option as shown below.

Now you will have a file name “hello.c.orig”, which is the backup of the original hello.c. You can also use -V to decide the backup filename format as shown below. Now you will have a file name “hello.c.~1~”.

6. Validate the Patch without Applying (Dry-run Patch File)

You can dry run the patch command to see if you are getting any errors, without patching the file using –dry-run option as shown below.

7. Reverse a Patch that is Already Applied (Undo a Patch)

You can notice from the filesize, that the patch, which is applied already is reversed when we used the -R option.

Источник

The “patch” Command in Linux [4 Practical Examples]

The patch command in Linux updates a file by identifying the changes required. It works with a patch file containing the difference between the original and modified files and applies these changes to update the original file. In simple words, the patch command is mainly used for updating and fixing bugs in software.

A. Description

The patch command in Linux updates any source code of any program by identifying the data that needs to be changed/updated from a new file.

B. Syntax

The patch command in Linux takes OPTIONS, ORIGINAL_FILE, and PATCHFILE as an argument. The syntax for the patch command is given below.

patch [OPTIONS] [ORIGINAL_FILE] [PATCHFILE]

Note: Here, the OPTIONS, ORIGINAL_FILE, and PATCHFILE enclosed by square brackets refer that OPTIONS, ORIGINAL_FILE, and PATCHFILE are not mandatory for the command.

C. Options

Different options can be added to the syntax of the patch command to modify the command. Here, I have listed some useful options below. If you do not find your desired option here, you can look for it on the man (manual) page. To go to the man page, type the following command and press ENTER.

  • -b/–backup: It creates backup files. When patching a file, rename or copy the original instead of removing it.
  • -c/–context: It specifies the number of context lines to be included in the patch file.
  • -d dir/–directory=dir: It changes to the directory “dir” immediately before doing anything else.
  • -D define/–ifdef=define: Uses the #ifdef#endif constructs to mark changes, with ‘define‘ as the differentiating symbol.
  • –dry-run: It prints the results of applying the patches without changing any files.
  • –help: It prints a summary of options and exit.
  • -v/–version: It prints out the revision header, patch level of the patch, and exit.
Читайте также:  Linux going to root directory

Note: All options in Linux are case-sensitive. Therefore, you must be careful while using these.

Practical Examples of the “patch” Command in Linux

I have two files named myfile.c and new_myfile.c.

The patch command is used to update a program. Moreover, The patch command in Linux has many practical applications, and a few of them are illustrated below. Here, I will work with the following two files named myfile.c and new_myfile.c.

Example 1: Applying the Patch File Using the “patch” Command in Linux.

You can easily update any change in your original file using the patch command in Linux. I will make a patch file and then apply this patch file to the source code. However, the source code and the patch file must exist in the same directory. To do so, follow the below procedures.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

➋ Then, type the following command to extinguish the difference between the two files named myfile.c and new_myfile.c.

diff -u myfile.c new_myfile.c > myfile.patch

Note: Here, the diff command finds the difference between the files named myfile.c and new_myfile.c. and redirects the output to the file named myfile.patch

➌ Now, press the ENTER button.

➍ Then, type the following command in the command prompt.

The patch command has created a patch file containing the updated data.

➎ Now, press the ENTER button.

Note: The following image shows that the patch command has created a patch file containing the updated data.

➏ Then type the following command in the command prompt.

➐ Now, press the ENTER button.

The patch command in Linux has applied the update to the source code.

The following image shows that the patch command in Linux has applied the update to the source code.

Similar Readings

Example 2: Backup Original File Before Applying the Patch Using the “patch” Command in Linux.

You can back up your original file before applying the patch using the -b option with the patch command in Linux. Here, I will create a backup patch file before applying the patch. To achieve so, follow the steps given below.

Steps to Follow:

Читайте также:  Получить имя компьютера linux

➊ At first, open the Ubuntu Terminal.

➋ Then type the following command in the command prompt.

➌ Now, press the ENTER button.

➍ Finally, type N and press the ENTER button to skip the reverse operation.

The patch command in Linux has created a backup of the original file before applying the patch, as depicted in the below image.The patch command in Linux has created a backup of the original file before applying the patch.Now you can verify the creation of a backup file using the ls command.A backup file exists in the current directory.

Example 3: Validate Patch Files Using the “patch” Command in Linux.

You can verify the outcome of the output of the patching without affecting the original source code. Here I will validate the output of patching without changing the original source code with the help of the –dry-run option of the patch command in Linux.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

➋ Then, type the following command in the command prompt.

➌ Now, press the ENTER button.

➍ Finally, type N and press the ENTER button to skip the reverse operation.

The --dry-run option of the patch command in Linux has validated the output of patching without changing the original source code.

The –dry-run option of the patch command in Linux has validated the output of patching without changing the original source code, as depicted in the below image.

Similar Readings

Example 4: Reverse or Undo a Patch Using the “patch” Command in Linux.

You can reverse or undo a patch that has already been applied using the patch command. Here, I will reverse the patch of myfile.patch on the source code. To do the task, follow the below procedures.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

➋ Execute the following command to apply the patch first.

➌ Then, type N and press the ENTER button to skip steps.

➍ Now, copy the given commands in the command prompt to display files and reverse the patch update.

➎ Finally, press the ENTER button for every command.

The patch command in Linux has done the job of reversing or undoing, which is why the size of the myfile.c file has lessened from 99 bytes to 65 bytes.

The following image shows that the patch command has done the job of reversing or undoing, which is why the size of the myfile.c file has lessened from 99 bytes to 65 bytes.

Conclusion

In this article, I have demonstrated the process of updating a program using the patch command in Linux. Therefore, I hope you’ll be competent enough to explore more things with the help of these illustrated practical examples.

Similar Readings

  • The “touch” Command in Linux [8 Practical Examples]
  • The “mkdir” Command in Linux [6+ Practical Examples]
  • The “locate” Command in Linux [7 Practical Examples]
  • The “find” Command in Linux [10+ Practical Examples]
  • The “chmod” Command in Linux [6 Practical Examples]
  • The “chown” Command in Linux [8 Practical Examples]

Источник

Оцените статью
Adblock
detector