Diff and patch

From Schmid.wiki
Jump to: navigation, search

Basic Usage

Create a directory patch:

cp -r dir dir-modified
# apply changes to dir-modified
diff -ru dir dir-modified > dir.patch

Then you can apply the patch:

cp -r dir dir-patched
cd dir-patched
patch -p1 <../dir.patch

Single File Patch

Example

First, we'll create some test output:

$ zcat /usr/man/man5/passwd.5.gz | groff -a | fmt -w30 | tail -n30 >original.txt

Then, we'll make a version with a few changes:

$ sed -e "s/ a / pjat /g" <original.txt >changed.txt

Let's take a look at the changes:

$ diff -y --width=70 original.txt changed.txt
        |
        |_ displays a fancy side-by-side format

Now let's make a file containing the changes:

$ diff original.txt changed.txt >pjat.patch

Make a copy of the orignal and patch it:

$ cp original.txt patched.txt
$ patch patched.txt pjat.patch

And check that it is patched correctly:

$ diff -s patched.txt changed.txt
Files patched.txt and changed.txt are identical
Personal tools