diff and patch Commands | Version Control

DevOps with Erdenay
2 min readDec 28, 2020

Hi! I am Erdenay Ateş and at this example we have got a script(usage_checker) for calculating using rate of disk space but it is not working quite well. And we asked to our friend for help. After that our friend fixed the problem and sent us with “usage_checker_fixed” name!
After that we should see and understand what have been changed at our script so we will use diff and patch commands at that story.

diff = shows us differences of two files
patch = transfers the differences

our first script with mistakes — usage_checker.sh
second version of script without mistakes — usage_checker_fixed.sh

Maybe it is easy to see the differences but that wont be easy to do that at many lines of code. If we type diff usage_checker.sh usage_check_fixed.sh output will be like:

“<” means content of first file(usage_checker.sh)
“>” means content of second file(usage_checker_fixed.sh)

(1) part says to us “differences at 2nd and 3th column” and (2) part says the same for 6th

If we save that output as file, we can use that file for transferring the differences to our first script;

diff usage_checker.sh usage_checker_fixed.sh > usage.diff

After that step we will use patch command like;

patch usage_checker.sh < usage.diff

That line means “take these differences and send them to first script!”
When we typed that and if everything is clearly done, we should see “patching file usage_checker.sh”
So if we type cat usage_checker.sh we can see our mission is completed!

last version of usage_checker.sh

Examples are here;
erdenayates/diff_and_patch (github.com)

--

--