diff和patch的使用简介

2023-10-29

diff的使用

我们先help看下diff的介绍

Usage: diff [OPTION]... FILES
Compare FILES line by line.
Mandatory arguments to long options are mandatory for short options too.
--normal                  output a normal diff (the default)
-q, --brief                   report only when files differ
-s, --report-identical-files  report when two files are the same
-c, -C NUM, --context[=NUM]   output NUM (default 3) lines of copied context
-u, -U NUM, --unified[=NUM]   output NUM (default 3) lines of unified context
-e, --ed                      output an ed script
-n, --rcs                     output an RCS format diff
-y, --side-by-side            output in two columns
-W, --width=NUM               output at most NUM (default 130) print columns
--left-column             output only the left column of common lines
--suppress-common-lines   do not output common lines
-p, --show-c-function         show which C function each change is in
-F, --show-function-line=RE   show the most recent line matching RE
--label LABEL             use LABEL instead of file name and timestamp
(can be repeated)
-t, --expand-tabs             expand tabs to spaces in output
-T, --initial-tab             make tabs line up by prepending a tab
--tabsize=NUM             tab stops every NUM (default 8) print columns
--suppress-blank-empty    suppress space or tab before empty output lines
-l, --paginate                pass output through 'pr' to paginate it
-r, --recursive                 recursively compare any subdirectories found
--no-dereference            don't follow symbolic links
-N, --new-file                  treat absent files as empty
--unidirectional-new-file   treat absent first files as empty
--ignore-file-name-case     ignore case when comparing file names
--no-ignore-file-name-case  consider case when comparing file names
-x, --exclude=PAT               exclude files that match PAT
-X, --exclude-from=FILE         exclude files that match any pattern in FILE
-S, --starting-file=FILE        start with FILE when comparing directories
--from-file=FILE1           compare FILE1 to all operands;
FILE1 can be a directory
--to-file=FILE2             compare all operands to FILE2;
FILE2 can be a directory
-i, --ignore-case               ignore case differences in file contents
-E, --ignore-tab-expansion      ignore changes due to tab expansion
-Z, --ignore-trailing-space     ignore white space at line end
-b, --ignore-space-change       ignore changes in the amount of white space
-w, --ignore-all-space          ignore all white space
-B, --ignore-blank-lines        ignore changes where lines are all blank
-I, --ignore-matching-lines=RE  ignore changes where all lines match RE
-a, --text                      treat all files as text
--strip-trailing-cr         strip trailing carriage return on input
-D, --ifdef=NAME                output merged file with '#ifdef NAME' diffs
--GTYPE-group-format=GFMT   format GTYPE input groups with GFMT
--line-format=LFMT          format all input lines with LFMT
--LTYPE-line-format=LFMT    format LTYPE input lines with LFMT
These format options provide fine-grained control over the output
of diff, generalizing -D/--ifdef.
LTYPE is 'old', 'new', or 'unchanged'.  GTYPE is LTYPE or 'changed'.
GFMT (only) may contain:
%<  lines from FILE1
%>  lines from FILE2
%=  lines common to FILE1 and FILE2
%[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTER
LETTERs are as follows for new group, lower case for old group:
F  first line number
L  last line number
N  number of lines = L-F+1
E  F-1
M  L+1
%(A=B?T:E)  if A equals B then T else E
LFMT (only) may contain:
%L  contents of line
%l  contents of line, excluding any trailing newline
%[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line number
Both GFMT and LFMT may contain:
%%  %
%c'C'  the single character C
%c'\OOO'  the character with octal code OOO
C    the character C (other characters represent themselves)
-d, --minimal            try hard to find a smaller set of changes
--horizon-lines=NUM  keep NUM lines of the common prefix and suffix
--speed-large-files  assume large files and many scattered small changes
--color[=WHEN]       colorize the output; WHEN can be 'never', 'always',
or 'auto' (the default)
--palette=PALETTE    the colors to use when --color is active; PALETTE is
a colon-separated list of terminfo capabilities
  --help               display this help and exit

-v, --version            output version information and exit
FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE' or 'FILE DIR'.
If --from-file or --to-file is given, there are no restrictions on FILE(s).
If a FILE is '-', read standard input.
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.

命令格式
diff [选项] old_file new_file
以上old_file 和new_file可以是文件,也可以是文件夹。
常用选项
选项 说明
-r 递归比较各子目录下的文件
-N 将不存在的文件当作空文件
-u 默认输出每个修改前后的3行,也可以用-uX来指定输出更多上下文。
-B 忽略对空行的比较
-w 忽略所有空格和制表符。例如,if ( a == b ) 与 if(a==b) 相等。

生成补丁的命令
diff -Nur src src_new >src.patch

patch的使用

先help看看patch的介绍

Usage: patch [OPTION]... [ORIGFILE [PATCHFILE]]
Input options:
-p NUM  --strip=NUM  Strip NUM leading components from file names.
-F LINES  --fuzz LINES  Set the fuzz factor to LINES for inexact matching.
-l  --ignore-whitespace  Ignore white space changes between patch and input.
-c  --context  Interpret the patch as a context difference.
-e  --ed  Interpret the patch as an ed script.
-n  --normal  Interpret the patch as a normal difference.
-u  --unified  Interpret the patch as a unified difference.
-N  --forward  Ignore patches that appear to be reversed or already applied.
-R  --reverse  Assume patches were created with old and new files swapped.
-i PATCHFILE  --input=PATCHFILE  Read patch from PATCHFILE instead of stdin.
Output options:
-o FILE  --output=FILE  Output patched files to FILE.
-r FILE  --reject-file=FILE  Output rejects to FILE.
-D NAME  --ifdef=NAME  Make merged if-then-else output using NAME.
--merge  Merge using conflict markers instead of creating reject files.
-E  --remove-empty-files  Remove output files that are empty after patching.
-Z  --set-utc  Set times of patched files, assuming diff uses UTC (GMT).
-T  --set-time  Likewise, assuming local time.
--quoting-style=WORD   output file names using quoting style WORD.
Valid WORDs are: literal, shell, shell-always, c, escape.
Default is taken from QUOTING_STYLE env variable, or 'shell' if unset.
Backup and version control options:
-b  --backup  Back up the original contents of each file.
--backup-if-mismatch  Back up if the patch does not match exactly.
--no-backup-if-mismatch  Back up mismatches only if otherwise requested.
-V STYLE  --version-control=STYLE  Use STYLE version control.
STYLE is either 'simple', 'numbered', or 'existing'.
-B PREFIX  --prefix=PREFIX  Prepend PREFIX to backup file names.
-Y PREFIX  --basename-prefix=PREFIX  Prepend PREFIX to backup file basenames.
-z SUFFIX  --suffix=SUFFIX  Append SUFFIX to backup file names.
-g NUM  --get=NUM  Get files from RCS etc. if positive; ask if negative.
Miscellaneous options:
-t  --batch  Ask no questions; skip bad-Prereq patches; assume reversed.
-f  --force  Like -t, but ignore bad-Prereq patches, and assume unreversed.
-s  --quiet  --silent  Work silently unless an error occurs.
--verbose  Output extra information about the work being done.
--dry-run  Do not actually change any files; just print what would happen.
--posix  Conform to the POSIX standard.
-d DIR  --directory=DIR  Change the working directory to DIR first.
--reject-format=FORMAT  Create 'context' or 'unified' rejects.
--binary  Read and write data in binary mode.
--read-only=BEHAVIOR  How to handle read-only input files: 'ignore' that they
are read-only, 'warn' (default), or 'fail'.
-v  --version  Output version info.
--help  Output this help.

获取补丁文件后
打补丁的命令

patch -pn < 补丁文件
如果已经打过补丁,会有如下提示
patching file src/a/b/c/d/file
Reversed (or previously applied) patch detected! Assume -R? [n]
选择y会还原

还原的命令

patch -Rpn < 补丁文件

p0表示不跳过任何目录以当前目录为基准。即从当前目录中查找src/a/b/c/d/file.

p1表示忽略当前目录的下一级目录,依此类推
不使用p参数 时候,patch命令会忽略所有斜杠,直接使用文件。直接在需要打补丁的文件目录下就可以不用待p参数直接
patch < 补丁文件即可

做个笔记防止后面又忘记了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

diff和patch的使用简介 的相关文章

  • 新的 Material Design 底部导航应使用哪个视图? [复制]

    这个问题在这里已经有答案了 我相信你们都听说过添加底部导航 https www google com design spec components bottom navigation html材料设计指南 我计划将其添加到我的应用程序中 但
  • 如何将库项目添加到 Android Studio?

    如何添加库项目 例如 Sherlock ABS 到安卓工作室 https en wikipedia org wiki Android Studio 不是针对旧的基于 Eclipse 的 ADT 捆绑包 而是针对新的 ADT安卓工作室 htt
  • 如何将 ThreeJS 与 PhoneGap 一起使用?

    这个探索是非常自我描述的 我已经用一个简单的 3D 立方体进行了测试 它在浏览器中运行良好 但只在模拟器中显示空白页面 有人说 Threejs 不能与 PhoneGap 一起使用 但也有人说他们使用过并且工作正常 在 Android 中 您
  • Android:对于具有 LinearLayout 定义的成员的 ListView,上下文菜单不显示?

    我有一个 ListActivity 和 ListView 并且我已将一些数据绑定到它 数据显示得很好 而且我还为视图注册了一个上下文菜单 当我将列表项显示为简单的 TextView 时 它工作正常
  • 检查从 arrayadapter 获取的复选框

    我有标题清单 CheckBox 我想控制默认检查哪一个 所以我试图获得正确的视图并检查它 但由于某种原因它不起作用 知道为什么吗 form checkbox item xml
  • Android - 如何在 ListView 的背景上显示垂直线(并根据行高)?

    如何在背景上显示一条垂直线 如下图蓝色突出显示的那条 在此示例中 我有一个带有 ImageView 元素的 ListView 和 TextView 但它与线条无关 并且我希望这些项目的背景上有一条垂直线 让人感觉它们与每个项目 连接 另请注
  • 为什么 Android 中奇怪的命名约定是“AlertDialog.Builder”而不是“AlertDialogBu​​ilder”

    Why not AlertDialogBuilder builder new AlertDialogBuilder this builder setTitle foo 代替 AlertDialog Builder builder new A
  • 为什么 Android Eclipse 不断刷新外部文件夹并花费很长时间?

    我只有一部新的 Android 手机 我一直在修补一些基本的应用程序 每当我保存任何内容时 Eclipse 的 Android 插件就会刷新外部文件夹 这让我抓狂 通常我不会介意 但当需要 10 秒才能刷新时 我开始注意到 我已经搜索过 其
  • overridePendingTransition 显示第二个活动的速度太快

    我有 2 个活动 我想在两个活动之间创建一个动画过渡 以便两个活动的视图向上滑动 就好像第二个活动正在向上推动第一个活动一样 在我的第一个活动中我使用 Intent iSecondActivity new Intent FirstActiv
  • Gradle 构建无法解析生成的类

    我刚刚将 Android Studio 从 1 5 更新到 2 2 1 Gradle 也是如此 我有一个项目 它使用注释处理器框架 由我制作 来执行类似 Dagger 的依赖注入 这在更新之前工作得很好 但现在 Gradle 无法解析生成的
  • 如何使用Multipart将图像上传到php服务器

    我一直很头疼 如何将图像上传到服务器 这对我来说是新任务 但对我来说很困惑 我在 stackoverflow 上搜索并用谷歌搜索 但我遇到了问题 我的意图是从 SD 卡上传照片并从相机拍照并上传到服务器 在ios中 这个任务已经完成 在io
  • 防止滚动时自定义 ListView 刷新。我想停止刷新

    我在我的 Android 应用程序中使用自定义 ListView 问题是当我滚动 ListView 时 它会自动清除从 EditText Edittext 作为 list item 输入的数据 并且 ListView 刷新 我想阻止它停止清
  • 在 Android 模拟器中获取互联网连接

    我有一台带有wifi连接的台式电脑 我的IP地址是192 168 12 95 网关是192 168 10 10 但是我在android模拟器中没有获得互联网连接 也就是说我无法访问internate 我也尝试过 emulator avd w
  • 如何在Android 11中获取dir文件列表

    我想编写自己的精简版文件浏览器 文件 API 现在不适用于外部存储 该版本还提供了对范围存储的改进 这使得开发人员可以更轻松地迁移到使用此存储模型 我不明白如何使用范围存储来访问 sdcard 如果您正在寻找文件选择器体验 存储访问框架 h
  • 屏幕方向更改后应用程序崩溃

    我有以下问题 启动后 应用程序工作正常 即使在更改屏幕方向后也是如此 应用程序尚未准备好处理方向更改 例如替代布局等 因此仅显示旋转的默认布局就可以了 但是 当我通过按后退键离开应用程序 更改方向并在再次启动应用程序后立即崩溃 崩溃后 如果
  • 自定义支持对话框片段文本颜色错误

    如果我放一个TextView在自定义对话框中 文本默认为黑色 不幸的是 支持片段对话框背景是深灰色的 灰底黑字很难读 我可以改变TextView白色或类似的东西android textColor android attr textColor
  • SQlite 获取最近的位置(带有纬度和经度)

    我的 SQLite 数据库中存储有纬度和经度的数据 我想获取距我输入的参数最近的位置 例如我当前的位置 纬度 经度等 我知道这在 MySQL 中是可能的 并且我已经做了相当多的研究 SQLite 需要一个自定义外部函数来实现半正弦公式 计算
  • 未捕获的引用错误:cordova 未定义

    这是我的 HelloPlugin js 文件 var HelloPlugin callNativeFunction function success fail resultType return cordova exec success f
  • 如何在ListView中标记视图?

    我有一个带有列表视图的应用程序 列表视图工作正常 当我希望列表以标记的某些行开始时 问题就开始了 如果我按下它 我可以标记一行 但是 似乎没有找到一种方法来在初始化时标记任何行 这是我的代码 listViewOfBluetooth getL
  • Android Webview:无法调用确定的可见性() - 从未见过 pid 的连接

    我有一个 Android Webview 当我单击链接下载文件 pdf 图像等 时 我收到一条错误消息 Error message Cannot call determinedVisibility never saw a connectio

随机推荐

  • Jenkins配置邮件发送测试报告

    我们用jenkins集成测试 Jenkins GitLab Python自动化测试持续集成 构建任务执行完成后 可以将测试结果通过邮件形式发送至相关人员 告知本次项目构建结果 一 开启邮箱SMTP服务 这里我用的是网易163邮箱 登录163
  • Unity 实现 角色的换装

    换装的三个要点 材质 网格 模型 unity中换装 即更改角色部位上的skinnedMeshRender组件的属性 更换mesh mesh 和骨骼的重新绑定 最后更换材质 一个模型 带有skinnedMeshRender组件 的子节点 和对
  • 灰灰-328-LeetCode682棒球比赛(vector、stack、atio()、substr()、c_str()、accumulate())

    你现在是棒球比赛记录员 给定一个字符串列表 每个字符串可以是以下四种类型之一 1 整数 一轮的得分 直接表示您在本轮中获得的积分数 2 一轮的得分 表示本轮获得的得分是前两轮有效 回合得分的总和 3 D 一轮的得分 表示本轮获得的得分是前一
  • SQLPro Studio for Mac(可视化数据库管理工具)

    SQLPro Studio for Mac是一款可视化数据库管理工具 为创建 MySQL MSSQL Oracle和Postgres连接提供支持的数据库管理解决方案 包括SSH隧道功能 SQLPro Studio为您提供了通过相同的用户界面
  • 华为免费虚拟服务器,免费试用虚拟服务器

    免费试用虚拟服务器 内容精选 换一换 本节操作介绍切换虚拟私有云的操作步骤 仅支持单网卡切换虚拟私有云 切换虚拟私有云会导致云服务器网络中断 切换虚拟私有云过程中 请勿操作云服务器的弹性公网IP 或对云服务器做其他操作 切换虚拟私有云后 云
  • OpenCV

    OpenCV Mat类的copyT clone 赋值的区别 1 clone 2 copyTo 3 等号 赋值 4 验证 先说一下Mat类的结构 Mat类我们可以分成两部分 头部分 矩阵数据部分 头部分 用于记录矩阵数据的大小 类型 数据指针
  • 遗传算法的概念和python实现

    遗传算法是一个非常经典的智能算法 主要用于解决优化问题 本文主要简单介绍一些原理 同时给出一个基于python实现的 用于解决实数内优化问题的模板 本文参考 原理 遗传算法入门详解 知乎 简单介绍 遗传算法就是借鉴生物学中的遗传 首先生成若
  • TCP三次握手详解

    一 什么是TCP三次握手 三次握手 Three way Handshake 是指建立一个TCP连接时 需要客户端和服务器总共发送3个包 三次握手的目的是连接服务器指定端口 建立TCP连接 并同步连接双方的序列号和确认号并交换 TCP 窗口大
  • 你不知道的JavaScript---------- 行为委托

    目录 Prototype 机制 面向委托的设计 类理论 委托理论 比较思维模型 JavaScript创建UI控件 控件创建渲染 ES5类继承形式 控件 类 类形式 委托控件对象 委托形式 更简洁的设计 更好的语法 内省 Prototype
  • C++ 条件编译指令和defined 操作符

    使用条件条件编译指令 可以限制程序中的某些内容要在满足一定条件下才参与编译 因此 可以利用条件编译指令使同一个源程序在不同的编译环境下产生不同的目标代码 在头文件中使用 ifdef和 ifndef是非常重要的 可以防止双重定义错误的出现 常
  • centos8安装docker

    执行yum install docker ce会报错Problem package docker ce 3 19 03 3 3 el7 x86 64 requires containerd io gt 1 2 2 3 but none of
  • Android中RecyclerView分页加载数据

    Android中RecyclerView分页加载数据 在Android开发中 RecyclerView是一个强大的视图容器 常用于展示大量数据 当数据量很大时 一次性加载所有数据可能会导致用户等待时间过长或者内存不足的问题 为了解决这个问题
  • 第十一届蓝桥杯 b组

    答案 3880 代码 package 第十一届蓝桥杯 public class Main01 public static void main String args int t 10000 int time 0 boolean b true
  • [深入浅出Cocoa]iOS网络编程之Socket

    深入浅出Cocoa iOS网络编程之Socket 罗朝辉 http blog csdn net kesalin CC 许可 转载请注明出处 更多 Cocoa 开发文章 敬请访问 深入浅出Cocoa CSDN专栏 http blog csdn
  • 表单+初部认识css

    表单
  • python找零钱程序-Python实现的一个找零钱的小程序代码分享

    Python写的一个按面值找零钱的程序 按照我们正常的思维逻辑从大面值到小面值的找零方法 人民币面值有100元 50元 20元 10元 5元 1元 5角 1角 而程序也相应的设置了这些面值 只需要调用函数时传入您想要找零的金额 程序会自动算
  • 本地项目HTTP,加载静态资源却是HTTPS的问题【已解决】

    本地项目HTTP 加载静态资源却是HTTPS的问题 已解决 参考文章 1 本地项目HTTP 加载静态资源却是HTTPS的问题 已解决 2 https www cnblogs com a record p 9067060 html 备忘一下
  • linux桌面卡死解决办法

    切换回命令行 ctl alt f1 重启桌面 sudo service lightdm restart 切换回桌面 ctl alt f
  • Excel 2016图表标题不能输入中文,图表一直闪动

    问题 最近使用excel2016 发现插入图表后 图表一直闪 无法更改标题或者其它操作 如下图所示 解决 依次选择 文件 gt 选项 gt 加载项
  • diff和patch的使用简介

    diff的使用 我们先help看下diff的介绍 Usage diff OPTION FILES Compare FILES line by line Mandatory arguments to long options are mand