Assignment - patching files

This assignment is about 2 related commands: diff and patch. They deal with text files.

    diff - takes the names of 2 files as parameters, compares them, and produces text output expressing the differences between them.

    patch - takes the name of a 1st file, plus that of a 2nd that contains output produced previously by the diff command. The diff command which produced the 2nd file had compared the 1st with some (unknown) 3rd file. patch reconstructs what the content of this 3rd file must have been, and outputs it.

Here's an example. Suppose we have 2 text files. Adhering to the above naming, they are our "1st file" and "3rd file." Their contents are:

1st file - "before" 3rd file - "after"
Mary had a little lamb,
Little lamb,
Little lamb,
Mary had a little lamb
Whose fleece was white as snow.
Mary had a leg of lamb,
Little lamb,
Little lamb,
Mary had a little lamb
Whose fleece was like the snow.

The command

    diff before after

produces the following output:

< Mary had a little lamb,
> Mary had a leg of lamb,
< Whose fleece was white as snow.
> Whose fleece was like the snow.

which expresses the differences in a way understood by both diff and patch. This output spills out onto the screen. Suppose I capture it into a file named diff.file as follows:

    diff before after > diff.file

Suppose you also possess a copy of before. You lack a copy of after. I, and you, want you to have a copy of after. Without sending you one, I can instead send you a copy of diff.file. If you then run the command:

    patch -b before diff.file -o after

it will output ( -o ) a file named after on your disk, identical to mine. 

I have now indirectly provided you with a copy of after.

Typically, if before is very large, diff.file is much smaller than after. So it's efficient to supply the smaller file. The situation in which you and I would have identical copies of a to-be-changed file is if that file is a "standard" constituent of a product, such as a linux distribution. Then the whole world has exact copies of that same file. (Usually source files in a programming language like c.) The file maintainer can make the difference file available via the internet and the whole world can avail itself of the maintainer's new version, by using the patch command against the old.

The assignment

A file containing Mary Had a Little Lamb named "mary-had-a-little-lamb" can be found on the unix server, per your instructor.

Copy it to your home directory.

Rename it to something.

In that copy use the vi editor to make the necessary changes that convert the above "before" to "after." (Namely, replace the first line's "a little lamb" with "a leg of lamb" instead; and the last line's "fleece was white as" with "fleece was like the" instead.) I want to get a copy of the modified poem from you, but not directly.

Generate the differences between the original version and your version using diff, and capture those differences into a file named "mydifferences".

Deposit the mydifferneces file into your home directory's "assignments" subdirectory.

How I will grade you: using patch I will apply your mydifferences file against my copy of mary-had-a-little-lamb, and test whether the result equates to the original poem with substitution of "fleece was like the snow" for "fleece was white as snow," and "a leg of lamb" for "a little lamb." (The grading is automated and depends on the specified file nomenclature and placement.) You can check your work by duplicating my grading process: patch mary-had-a-little-lamb with mydifferences and make sure you get the results I expect.

obtain file blink.c from the server. It makes the cursor blink. In a terminal window (within the graphical interface) compile it.

gcc  blink.c  -o  blink

Run it


observe the cursor blink, and the blink speed. Obtain from the instructor a diff file (named diff.blink-blinkslow) to re-code blink.c so that it will slow down. Apply it:

patch -b blink.c  diff.blink-blinkslow

Now recompile the program:

gcc  blink.c  -o  blinkslow

and re-run, noting the blink speed:


Obtain files more01.c and longfile from the server. more01 is a primitive, amateur attempt to duplicate the behavior of the well-known more utility. Compile more01.c:

gcc  more01.c  -o  more01

Observe the behavior of giving text via standard input to both the real more program, and the amateur one:

cat  longfile  |  more

cat  longfile  |  ./more01

The amateur program doesn't do what is should-- stop screen scroll-off after a screenful of text has been diplayed. The real more program, of course, does. A version 2 of the amateur program fixes this.

Obtain from the instructor a diff file (named diff.more01-more02) to re-code more01 so that it will halt display after the first screenful. Apply it:

patch -b more01.c  diff.more01-more02

and build a new executable:

gcc  more01.c  -o  more02

Test the result to see that the problem is fixed:

cat  longfile  |  ./more02

making sure it halts the display as it should.

Additional: obtain and examine one of the recent patch files for the linux kernel. Visit There, navigate down to Scoll to the bottom to find patch files, with names such as patch-3.10.13.gz. Download one of these and open it in less, or an  editor. The patch file is more complicated than the simple one you produced, but diff understands it and can use it to transform the source files for one version of linux to those of the next.