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?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

How Linux Works

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

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Ad