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?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

The Linux Command Line

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

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Ad