How to use diff and patch together – Guide
Linux administration requires that you know a large number of commands. Some commands work separately to serve specific functions; other commands work together to create useful and powerful tools. One such combination is diff and patch, which can be used together to create a strong patch for Linux systems. Before using these two commands together, understand how to use each individually.
What is diff?
The commonly used diff command does a line-by-line comparison of two files. However, the output of the diff command can look confusing to those unfamiliar with it. To test the diff command, start by creating a file with the command: nanotest1 In this file, write a list numbered from 1 to 11, omitting the numbers 5 and 10: 1 two 3 4 6 7 8 9 11 Save and close the file. Then create a second file with the command: nanotest2 In this file, write the numbers from 1 to 10 without skipping any number: 1 two 3 4 5 6 7 8 9 10 Save and close the file. Now you can use the diff command to compare the two files and find the differences between them. Format your diff command like this: diff test1 test2 The output for the diff command you entered should be: 4 to 5
5 9c10 <11 – 10 To understand this, you must first understand what the letters c, a and d mean within the context of a diff output. C represents content that has been replaced, a represents added or appended content, and d represents deleted content. Examine these lines in groups. These first two lines mean that to make the two files match, you must add the number 5 after line 4 in the first file: 4 to 5 5 These next four lines mean that to match the files, you must also add number 11 in the second file after the ninth line and then add the number 10 in the first file after the tenth line in the second file: 9c10 <11 – 10 If you have two different files for the same code, you will need to find out what the differences are to create a patch.
Using diff and patch together
Diff catalogs change between two files and the patch takes those changes, puts them in a file, and updates previous versions of the files with those changes. For example, consider the following two files: original code contains the phrase Here are some words. updated code contains the phrase Here are some more words. The files are similar, but the update contains a slight difference from the original and you must patch these two files. To do this, start by entering the following code: diff -u original-code-updated-code> patchfile.patch If you look at the contents of patchfile.patch, you should find: – original code 2021-05-06 12:54: 41.531836242 -0400 +++ code updated 2021-05-06 12:54: 59.523750129 -0400 @@ -1 +1 @@ -Here are some words.
- Here are some more words. At this point, the patch command can use this .patch file to update the original. To do this, issue the command: patch original code patchfile.patch If you look at the contents of the original code, it should now match the updated code file perfectly. This allows you to create an efficient system where you can update code files quickly and easily via Linux commands.
Final note
I hope you like the guide How to use diff and patch together. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.