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.

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

Take your Linux skills to the next level!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Ad