Create Patch Files Using Diff And Patch Example
Using diff to create batch files and subsequently running them against select files is such a convenient way to update, fix or change existing files, but many developers and system administrators simply don’t know much about them.
Here is a quick diff and patch example, how to use diff and apply changes for select files. Use diff –help to check out more options and flags.
NOTE: pay caution when using patching, any mistake or error are executed without warning or undo feature. It’s best to always make a backup of any file or folder that are going to be affected by your patch.
diff example
Some useful flags you can specify when comparing files are -b (ignore white space difference), -B (ignore blank lines), -r (recursive) and -i (ignore case).
Let’s say we need to change the GA analytics code on our pages and that they are unfortunately hard coded. The files are shown below as are the results from diff:
File GA_v1:
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-9876543-21']); _gaq.push(['_setDomainName', '.domain.com']); _gaq.push(['_trackPageview']);
(function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl ' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();
File GA_v2:
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-1234567-89']); _gaq.push(['_trackPageview']);
(function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();
File GAold.patch, resulting from running diff -bi GA_v1 GA_v2 > GA_v2.patch:
2c2,3 < _gaq.push(['_setAccount', 'UA-1234567-89']); --- > _gaq.push(['_setAccount', 'UA-9876543-21']); > _gaq.push(['_setDomainName', '.domain.com']); 7c8 < ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; --- > ga.src = ('https:' == document.location.protocol ? 'https://ssl <https://ssl/> ' : 'http://www') + '.google-analytics.com/ga.js';
“2c2,3” = copy line 2 (from GA_v1) to line 2 (of GA_v2), insert line 3
“7c8” = copy line 7 to line 8
(c – copy, d – delete, a – add)
patch example
And with this file available, we now can use patch to perform the changes. One really important flag is –-dry-run It does not actually change any files, just print what would happen. And -b will make a backup of your files. Please use it especially if you’re new to patching.
patch --dry-run GA_v1 < GA_v2.patch
If you get no errors, you’re good to go.
patch -b GA_v1 < GA_v2.patch (using -b to make a backup)
Doing a diff of GA_v1 and GA_v2 shows a difference of 1 empty line. We could have avoided that using the -B flag when creating the patch file using diff, but there are many more options for both, diff and patch commands and we encourage you to explore them.