diff and patch

Published on 2006-05-22.

A mini tutorial in the usage of diff and patch.

Create the patch file:

$ diff -u old-file.txt new-file.txt > changes.diff

Use the patch in order to change old-file.txt to new-file.txt:

$ patch old-file.txt changes.diff

Or simply:

$ patch < changes.diff

patch will rename old-file.txt to old-file.txt.orig and save the new file with the changes as old-file.txt. old-file.txt is now identical to new-file.txt

If you want to reverse the process you can do it with:

$ patch -R old-file.txt changes.diff

Or simply:

$ patch -R < changes.diff

diff can also be used to display the difference between content in directories.

$ diff /home/user/foo /home/user/bar