Training Missions

Missions: Using diff and patch

Quick reference

Working with patch and diff

When working with patch, you are dealing with a patch file, which contains a list of differences produced by the diff program. The patch file applies those differences to one or more original files, producing patched versions.

In contrast, diff outputs the difference between two files by comparing both files line by line.

This page is just a quick reference; see the bottom for other resources.


To work with patch, you can use:


patch [options] [originalfile [patchfile]]

but the more common method is:

patch -pnum < patchfile.patch

-pnum is the option to tell patch how many levels of directories to ignore so it finds the correct files to patch.

To work with diff, you can use:

diff -u original updatedfile

For Windows users, you can redirect the output of your diff to a new file with this command:

diff -u original new > output.txt

-u is the number of lines of unified context to output. You can specify a number by putting it after this option (e.g. -u 12). If no number is specified, it will use the default of 3.

-ur is used when comparing directories of unified context by recursively comparing any subdirectories found.

Other resources


Patch is an efficent method to fix errors in text files. The following sites have further information about patch:

There are many options for diff, which are detailed in diff's documentation. The following sites are good resources for further reading:

Working with tarballs