Getty Images

Tip

An introduction to using diff and patch together

Use the diff command in Linux to discover subtle differences between code files. Then, use the patch command to update those code files to match.

Linux administration requires you to know a large number of commands. Some commands work separately to serve specific functions; other commands work together to create powerful and useful tools. One such combination is diff and patch, which can be used together to create a strong patch for Linux systems.

Before using these two commands together, understand how to use each one individually.

What is diff?

The commonly used diff command does a line-by-line comparison of two files. However, the output from the diff command can appear confusing for those unfamiliar with it.

To test out the diff command, start by creating a file with the command:

nano test1

In that file, write a numbered list of 1 through 11, leaving out the numbers 5 and 10:

1
2
3
4
6
7
8
9
11

Save and close the file.

Next, create a second file with the command:

nano test2

In that file, write out the numbers 1 through 10 without skipping any numbers:

1
2
3
4
5
6
7
8
9
10

Save and close the file.

Now you can use the diff command to compare the two files and find the differences between them. Format your diff command like this:

diff test1 test2

The output for the diff command you entered should read:

4a5
> 5
9c10
< 11
---
> 10

To understand this, you must first understand what the letters c, a and d mean within the context of a diff output. C represents content that has been replaced, a represents added or appended content and d stands for deleted content.

Examine these lines in groups. These first two lines mean that in order to make both files match, you must add the number 5 after line 4 in the first file:

4a5
> 5

These next four lines mean that in order to make the files match, you must also add the number 11 in the second file after the ninth line and then add the number 10 in the first file after the tenth line in the second file:

9c10
< 11
---
> 10

If you have two different files for the same code, you must find out what the differences are in order to create a patch.

Using diff and patch together

Diff catalogs changes between two files, and patch uses those changes, puts them into a file and updates older versions of files with those changes.

For example, consider the following two files:

  • original-code contains the phrase Here are a few words.
  • updated-code contains the phrase Here are a few more words.

The files are similar, but the update contains a slight difference compared to the original and you must generate a patch for these two files. To do that, start by entering the following code:

diff -u original-code updated-code > patchfile.patch

If you examine the contents of patchfile.patch, you should find:

--- original-code 2021-05-06 12:54:41.531836242 -0400
+++ updated-code 2021-05-06 12:54:59.523750129 -0400
@@ -1 +1 @@

-Here are a few words.
+Here are a few more words.

At this point, the patch command can use this .patchfile to update the original. To do this, issue the command:

patch original-code patchfile.patch

If you examine the contents of original-code, it should now perfectly match the updated-code file. This enables you to create an efficient system where you can quickly and easily update code files through Linux commands.

Dig Deeper on Data center ops, monitoring and management

SearchWindowsServer
Cloud Computing
Storage
Sustainability and ESG
Close