Linux下的diff和patch命令


Linux下的diff和patch命令
1.Patch命令的基本语法

The patch command takes a patch file patchfile containing a difference listing produced by the diff program and applies those differences to one or more original files, producing patched versions. Normally the patched versions are put in place of the originals.(patch是通过由diff命令产生的记录文件之间不同之处的补丁文件;默认情况下是在原文件的基础之上进行修改的)

The basic syntax is as follows:

$ patch < patch.file
$ patch source.code.file < patch.file
$ patch -p LEVEL < {/path/to/patch/file}

To apply a patch, one could run the following command in a shell:

$ patch < /path/to/file

In this example, patch foo.c with patch.diff file:

$ patch foo.c < patch.diff

Patches can be undone, or reversed, with the ‘-R’ option(使用”-R”选项撤销patch操作):

$ patch -R < /path/to/file
2.创建patch补丁文件

How do I create a patch? To create a patch, one could run the following diff command:

$ diff -u oldfile-name-here newfile-name-here > patch.diff
3.如何给一个源码树打补丁?

A note about working on an entire source tree

First, make a copy of the source tree:

## Original source code is in lighttpd-1.4.35/ directory ##
$ cp -R lighttpd-1.4.35/ lighttpd-1.4.35-new/

Cd to lighttpd-1.4.35-new directory and make changes as per your requirements:

$ cd lighttpd-1.4.35-new/
$ vi geoip-mod.c
$ vi Makefile

Finally, create a patch with the following command:

$ cd ..
$ diff -rupN lighttpd-1.4.35/ lighttpd-1.4.35-new/ > my.patch

You can use my.patch file to patch lighttpd-1.4.35 source code on a different computer/server using patch command as discussed above:

$ patch -p1 < my.patch

 

参考链接:
, ,

《 “Linux下的diff和patch命令” 》 有 4 条评论

  1. diff和patch命令
    http://www.magicsite.cn/blog/Linux-Unix/Linux/Linux136148.html
    `
    第一:如果当前目录下有linux-3.0源码目录文件和修改后的linux-3.0-s3c2440目录文件,如何生成patch文件?

    第二:如果当前目录下有修改后的linux-3.0-s3c2440目录文件和linux-3.0-s3c2440.patch文件,如何生成源码linux-3.0源码目录文件呢?

    第三:如果当前目录下有linux-3.0源码目录文件和linux-3.0-s3c2440.patch文件,如何生成修改后的linux-3.0-s3c2440目录文件呢?

    第四:如果你在linux-3.0源码中,linux-3.0-s3c2440.patch在/home/fulinux/下,如何生成修改后的linux-3.0-s3c2440目录文件呢?
    `

    linux patch补丁命令的用法
    http://www.xfcodes.com/linuxcmd/mulu/12091.htm
    `
    diff -Nru old_dir/ new_dir/ > dir.patch
    cp -r old_dir old_dir_bak #先备份一下老目录
    mv new_dir new_dir_bak #然后重命名一下新目录
    patch -p0 < dir.patch #old_dir的内容会变成new_dir里的
    diff -Nru old_dir/ new_dir_bak #测试一下是否如此
    `

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注