10. Patching from a diff file patch

The diff command is used by software developer to check for differences in source code. The output of a diff command can be used to patch files.

Patches are used to convert one version of a file to another. When source codes need updating, patch files are sent instead of the entire source code, as this saves bandwidth and download time.

The command to apply a patch is patch.

Applying a patch

To apply a patch, first run the diff command in unified mode (using the -u option).

$ cat file1.txtLife goals
1) Graduate High School
2) Go skydiving
3) Help and feed the poor and hungry
4) Run a half marathon
5) Swim at the beach
6) Travel to Ireland
7) Get rock solid abs
$ cat file2.txt Life bucket list 1) Graduate college 2) Go skydiving 3) Help and feed the poor 4) Run a marathon 5) Swim at the beach 6) Travel the world
$ diff -u file1.txt file2.txt > update.diffpatching file file1.txt
$ cat file1.txt # Now it's updated to file2.txt! Life bucket list 1) Graduate college 2) Go skydiving 3) Help and feed the poor 4) Run a marathon 5) Swim at the beach 6) Travel the world

Reversing a patch

To reverse a patch, use the -R mode.

$ patch -R < update.diff
patching file file1.txt
$ cat file1.txt
# Back to original file
 
Life goals
1) Graduate High School
2) Go skydiving
3) Help and feed the poor and hungry
4) Run a half marathon
5) Swim at the beach
6) Travel to Ireland
7) Get rock solid abs

Make sure that no changes were match to the updated file1.txt, as this will mess up the line numbers specified on the diff file.

Take your Linux skills to the next level!

System Admin Handbook

Take your Linux skills to the next level! Try Linux & UNIX

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Ad