Movatterモバイル変換


[0]ホーム

URL:


SearchDataCenter

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.

Jack Wallen
By
Published:28 Sep 2021

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 isdiff 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 useddiff command does a line-by-line comparison of two files. However, the output from thediff 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:

1234678911

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:

12345678910

Save and close the file.

Now you can use thediff command tocompare the two files and find the differences between them. Format yourdiff command like this:

diff test1 test2

The output for the diff command you entered should read:

4a5> 59c10< 11---> 10

To understand this, you must first understand what the letters c, a and d mean within the context of adiff output.C represents content that has been replaced,a represents added or appended content andd 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 tocreate a patch.

Using diff and patch together

Diff catalogs changes between two files, andpatch 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 phraseHere are a few words.
  • updated-code contains the phraseHere 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 ofpatchfile.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, thepatch 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 oforiginal-code, it should now perfectly match theupdated-code file. This enables you to create an efficient system where you can quickly and easily update code files throughLinux commands.

Dig Deeper on Data center ops, monitoring and management

Sponsored News
Related Content
SearchWindowsServer
SearchCloudComputing
SearchStorage
Sustainability
and ESG
Close

[8]ページ先頭

©2009-2026 Movatter.jp