Missions: Using diff and patch
Diffing entire directories
Diffing entire directories
With pancake success under your belt, your publisher invited you to write a new book: Things to eat with oven pancakes.
This second book was a smash-hit in England! Your publisher has asked you to prepare an edition for the United States of America.
Wise yet shrewd, you realize that the only change you'd need to make to your cookbook would be to replace every occurrence of the word "aubergine" with "eggplant". To make life easy for your publisher, you decide to submit a recursive diff that contains just this change, applied to all the files in your cookbook.
Here is your mission:
- Download this tarball containing your work targeted at the UK.
- Unpack the tarball, and make a second copy.
- In that second copy, find all places where "aubergine" is mentioned and replace them with the synonym, "eggplant". (If you see "Aubergine" with a capital "A", replace that with a similarly-capitalized word!)
- Make a unified diff of your changes. It should be possible to apply it with "patch -p1" from the "recipes" directory. (One way to do this is
diff -ur recipes recipes-us, where
recipesis an unmodified copy of the
Since this is a larger patch, you will be uploading it as a file rather than just pasting it in. (Also, if you're in the mood for some cooking after you complete the mission, we'd love to know how one of those recipes turns out. But that's not part of the mission.)
Context in open source
When contributing changes to a project, often your changes will involve more than one file, so it makes sense to submit all of those changes in one shot, especially if the changes depend on one another or it is the same adjustment to a number of files.
If you need help with this step, try clicking through these hints.
The "low" hint level is perfect if you're just confused as to what to do.
The "high" level is great if you are unfamiliar with the commands and tools you'd use.
or, if you're on Linux or Mac, use the terminal command:
In this mission, you have to do a few things. Tips:
- Make sure to keep a copy of the original around while modifying a copy!
- Use diff -ur to create the diff. You need -u for a "unified" diff, and -r to "recursively" generate a diff across a whole directory of files. (You can combine these parameters into just -ur.)
Open a command prompt, and type these commands in, one at a time. You need to copy them exactly. Make sure to press enter between each one.
First, create a directory you'll work in and change into it.
mkdir recursivediff cd recursivediff
Then, download the original collection of recipes.
curl -0 https://openhatch.org/missions/diffpatch/diffrecursive/recipes.tar.gz
Unzip the archive file, but do not extract it.
gzip -d recipes.tar.gz
Unpack it once, and rename that to recipes-us indicating that this is the version you'll adjust for American tastes. (This is the copy we'll edit.)
tar -xvf recipes.tar mv recipes recipes-us
Unpack it again, keeping this one with the original name. (We'll need this original so that diff can detect the differences.)
tar -xvf recipes.tar
Look for the new recipes-us directory on your computer, and open up each of the files inside ending in .txt. Change "aubergine" to "eggplant", and change "Aubergine" to "Eggplant" (capitalization matters!).
Go back to the terminal and run this command.
diff -ur recipes recipes-us > adjust-for-americans.patch
In the form above, upload the new adjust-for-americans.patch file. Then, submit!