Missions: Using diff and patch
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:
-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.
Patch is an efficent method to fix errors in text files. The following sites have further information about patch:
- Wikipedia has some good information about the patch function.
- Patch comes with a user "manual." You can read it by typing man patch in a terminal, or by visiting this page.
There are many options for diff, which are detailed in diff's documentation. The following sites are good resources for further reading:
- Wikipedia has some good information about the diff function.
- Diff also comes with a user manual. You can read it by typing man diff in a terminal, or by visiting this page.