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.
man patch
Useful Options
- -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.
Note: All options in Linux are case-sensitive. Therefore, you must be careful while using these.
Practical Examples of the “patch” Command in Linux
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.
cat myfile.patch
➎ 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.
patch < myfile.patch
➐ Now, press the ENTER button.
Output >
The following image shows that the patch command in Linux has applied the update to the source code.
Similar Readings
- The “pwd” Command in Linux [4 Practical Examples]
- The “cd” Command in Linux [6 Practical Examples]
- The “ls” Command in Linux [7+ Practical Examples]
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 >
➊ At first, open the Ubuntu Terminal.
➋ Then type the following command in the command prompt.
patch -b < myfile.patch
➌ Now, press the ENTER button.
➍ Finally, type N and press the ENTER button to skip the reverse operation.
Output >
The patch command in Linux has created a backup of the original file before applying the patch, as depicted in the below image. Now you can verify the creation of a backup file using the ls command.
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.
patch --dry-run < myfile.patch
➌ Now, press the ENTER button.
➍ Finally, type N and press the ENTER button to skip the reverse operation.
Output >
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
- The “mv” Command in Linux [8 Practical Examples]
- The “cp” Command in Linux [6 Practical Examples]
- The “rm” Command in Linux [7 Practical Examples]
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.
patch < myfile.patch
➌ 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.
ls -l myfile.c
patch -R < myfile.patch
ls -l myfile.c
➎ Finally, press the ENTER button for every command.
Output >
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]
FUNDAMENTALS A Complete Guide for Beginners