简介

diff以行为单位比较不同ASCII文件差异,可以输出一组指令,用于指导如何更改一个文件使其与第二个文件相同。diff在软件开发时多用于比较新旧版本代码,和patch连用可以将文件间区别做成补丁。

参考:Beginner's Guide to Installing from Source Patching一节

diff

用法

diff [-bBi] from-file to-file

选项与参数

from-file :源文件
to-file :目标文件

-b :忽略一行当中,仅有多个空白的差异(例如 "about me" 与 "about me" 视为相同
-B :忽略空白行的差异。
-i :忽略大小写的不同。

patch

制作补丁

diff -Naur passwd.old passwd.new > passwd.patch

补丁文件一般都以.patch结尾

升级

patch -pN < patch_file

还原

patch -R -pN < patch_file

选项与参数:

-p :后面可以接『取消几层目录』的意思。

-R :代表还原,将新的文件还原成原来旧的版本


使用举例

下面这些内容,基本上是教你如何看diff对比输出,意义不大,既然有beyondcompare,谁还费这麻烦。

准备实验数据

[root@localhost ~]# cat file1.txt
I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.
[root@localhost ~]# cat file2.txt
I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

使用diff自动为我们显示两个文件之间哪些行不同:

[root@localhost ~]# diff file1.txt file2.txt
,4c2,
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
---
> I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.

我们来看看这个输出是什么意思。 需要记住的重要一点是,当diff向你描述这些差异时,他的目的是:告诉你如何改变第一个文件使其与第二个文件相匹配。

diff输出的第一行将包含:

对应于第一个文件的行号,
一个字母(a用于添加,c用于改变,d用于删除)
对应于第二个文件的行号。

“2,4c2,4”表示:“第一个文件中的第2行到第4行需要更改以匹配第二个文件中的第2行到第4行。” 然后它会告诉我们每个文件中的内容。前面有<的行是第一个文件的行;前面有>的行是第二个文件的行;

"---"仅仅是用于分割file1.txt和file2.txt 这两个文件

另一个例子

[root@localhost ~]# cat file1.txt
I need to go to the store.
I need to buy some apples.
When I get home, I'll wash the dog.
[root@localhost ~]# cat file2.txt
I need to go to the store.
I need to buy some apples.
Oh yeah, I also need to buy grated cheese.
When I get home, I'll wash the dog.
[root@localhost ~]# diff file1.txt file2.txt
2a3
> Oh yeah, I also need to buy grated cheese.

输出告诉我们“在第一个文件的第二行之后,需要添加一行:从第二个文件开始的第三行。” 然后它向我们展示了要在第一个文件添加的这一行是什么。

另一个例子

[root@localhost ~]# cat file1.txt
I need to go to the store.
I need to buy some apples.
When I get home, I'll wash the dog.
I promise.
[root@localhost ~]# cat file2.txt
I need to go to the store.
I need to buy some apples.
When I get home, I'll wash the dog.
[root@localhost ~]# diff file1.txt file2.txt
4d3
< I promise.

输出告诉我们“您需要删除第一个文件中的第4行,以便两行文件在第3行同步。” 然后它向我们显示需要删除的行的内容。

在上下文中查看差异输出

上面的例子显示了diff的默认输出。 它的目的是被计算机阅读,而不是人类阅读,因此对于人类目的而言,有时候可以看到更改的上下文。

GNU diff是大多数Linux用户将使用的版本,它提供了两种不同的方法来实现这一点:“上下文模式”和“统一模式”。

要在上下文模式下查看差异,请使用-c选项。 例如,假设file1.txt和file2.txt包含以下内容:

[root@localhost ~]# cat file1.txt
apples
oranges
kiwis
carrots
[root@localhost ~]# cat file2.txt
apples
kiwis
carrots
grapefruits
[root@localhost ~]# diff -c file1.txt file2.txt
*** file1.txt -- ::16.841974345 -
--- file2.txt -- ::45.919587320 -
***************
*** , ****
apples
- oranges
kiwis
carrots
--- , ----
apples
kiwis
carrots
+ grapefruits

前两行:***标识源文件,---标识目标文件,然后又分别跟文件名,修改日期时间

"***************" 仅仅是分隔符方便阅读

***第一个文件行的范围****

第一个文件每行的内容。如果这行没变化,则以2个开头。如果这一行变化了,则一个标识字符+开头

标识字符如下:

---第二个文件行的范围----

Unified Mode

[root@localhost ~]# diff -u file1.txt file2.txt
--- file1.txt -- ::16.841974345 -
+++ file2.txt -- ::45.919587320 -
@@ -, +, @@
apples
-oranges
kiwis
carrots
+grapefruits

这里差异显示我们一个文本,而不是两个单独的文本。

@@ -1,4 +1,4 @@

-1,4表示第一个文件1-4行,+1,4表示第二个文件1-4行

生成脚本

-e选项通过diff来输出一个脚本,该脚本可以被编辑程序ed或ex使用,该脚本包含一系列命令。 这些命令是c(change),a(add)和d(delete)的组合。

[root@localhost ~]# cat file1.txt
Once upon a time, there was a girl named Persephone.
She had black hair.
She loved her mother more than anything.
She liked to sit outside in the sunshine with her cat, Daisy.
She dreamed of being a painter when she grew up. [root@localhost ~]# cat file2.txt
Once upon a time, there was a girl named Persephone.
She had red hair.
She loved chocolate chip cookies more than anything.
She liked to sit outside in the sunshine with her cat, Daisy.
She would look up into the clouds and dream of being a world-famous baker.
[root@localhost ~]# diff -e file1.txt file2.txt
,6c
She would look up into the clouds and dream of being a world-famous baker.
.
,3c
She had red hair.
She loved chocolate chip cookies more than anything.
.

也可以将差异内容重定向到文件,方便ed使用

[root@localhost ~]# diff -e file1.txt file2.txt > my-ed-script.txt
[root@localhost ~]# cat my-ed-script.txt
,6c
She would look up into the clouds and dream of being a world-famous baker.
.
,3c
She had red hair.
She loved chocolate chip cookies more than anything.
.

还差一步,需要在脚本中追加w,表示 编辑写入实际文件

[root@localhost ~]# echo "w" >> my-ed-script.txt

下面命令就可以修改原始文件

ed - file1.txt < my-ed-script.txt

-指示ed从标准输入读取,<将my-ed-script.txt重定向为标准输入。这条命令执行完后,源文件file1.txt就被修改了。

 并排显示差异

[root@localhost ~]# diff -y file1.txt file2.txt
Once upon a time, there was a girl named Persephone. Once upon a time, there was a girl named Persephone.
She had black hair. | She had red hair.
She loved her mother more than anything. | She loved chocolate chip cookies more than anything.
She liked to sit outside in the sunshine with her cat, Daisy. She liked to sit outside in the sunshine with her cat, Daisy.
She dreamed of being a painter when she grew up. | She would look up into the clouds and dream of being a world-
<

Linux命令——diff、patch的更多相关文章

  1. Linux命令 diff cmp patch

    diff: 以行为单位进行比对 $ cat passwd | sed -e '4d' -e '6c no six line' > passwd.new $ cat -n passwd.new 1 ...

  2. linux命令-diff对比文件工具

    diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...

  3. Linux下diff与patch命令的配合使用

    在Linux下,diff与patch命令配合使用可以进行简单的代码维护工作. [A] diff diff命令用于比较文件的差异,可以用于制作patch文件.但此命令参数众多.格式多样,所以在此仅介绍较 ...

  4. diff命令和patch命令

    diff命令和patch命令 Linux就这个范儿 2.9.5 文件对比命令——diff diff命令搭建网站离不开数据库,在Linux系统上我们使用源码安装了MySQL服务器.不久我们发现 Goog ...

  5. Linux之旅(1): diff, patch和quilt (下)

    Linux之旅(1): diff, patch和quilt (下) 2 quilt 我们自己的项目能够用cvs或svn管理所有代码.但有时我们要使用其它开发人员维护的项目.我们须要改动一些文件,但又不 ...

  6. Linux游(1): diff, patch和quilt (下一个)

    Linux游(1): diff, patch和quilt (下一个) 2 quilt 我们自己的项目可以用cvs或svn管理所有代码.但有时我们要使用其它开发人员维护的项目.我们须要改动一些文件.但又 ...

  7. Linux下一个patch补丁命令

    此命令用于为特定软件包打补丁,他使用diff命令对源文件进行操作. 基本命令语法: patch [-R] {-p(n)} [--dry-run] < patch_file_name p:为pat ...

  8. Linux下Diff命令

    一般正常比较两个文件用vimdiff,算是直接进入vim界面,如果比较两个文件夹下面的文件,可以用diff,注意,这里只会比较文件夹下面的同名文件,他会列出不一样的点. 参考Linux下Diff命令使 ...

  9. Linux diff patch

    /***************************************************************************** * Linux diff patch * ...

随机推荐

  1. Ubuntu-18.04更改安装源为国内源

    环境查看 修改源文件 /etc/apt/sources.list 备份配置文件后把文件内容替换如下 deb http://mirrors.aliyun.com/ubuntu/ bionic main ...

  2. CEIWEI CheckSum CRC校验精灵v2.1 CRC3/CRC4/CRC5/CRC6/CRC8CRC10/CRC11/CRC16/CRC24/CRC32/CRC40/CRC64/CRC82/Adler32

    CEIWEI CheckSum CRC校验精灵 是一款通用的循环冗余校验码CRC(Cyclic Redundancy Check).MD5.SHA1.SHA2.SHA3.HAVAL.SHAKE.TIG ...

  3. VS2017.常量中有换行符

    1.VS中加入 “/utf-8” Qt中 也是加入“/utf-8”,加的地方注意下:在 pro文件中 ,这个位置加入: win32-msvc*:QMAKE_CXXFLAGS += /wd"4 ...

  4. 谷歌插件:右键搜使用指南.md

    基本功能看插件,说几个自定义搜索的实现 自定义技巧:%s选择的文字或页面或链接,%g转换成GBK字符集,%t转换成BIG5字符集,%p剪贴板内容,%u当前页面域名. 注:这部分内容来自谷歌插件介绍.使 ...

  5. location匹配禁止页面缓存

    php禁止页面缓存的办法 //设置此页面的过期时间(用格林威治时间表示),只要是已经过去的日期即可. add_header Expires: Mon, 26 Jul 1997 05:00:00 GMT ...

  6. python从hello world开始 - python基础入门(3)

    万丈高楼平地起,编程亦如此.改变世界是结果,坚持努力学习改bug是过程,hello world是开始,所有语言均是如此. 一.使用pycharm创建第一个hello world 项目 1.Create ...

  7. [转帖]删除一张大表时为什么undo占用空间接近原表两倍?

    删除一张大表时为什么undo占用空间接近原表两倍? https://www.toutiao.com/i6736735016492990983/ 原创 波波说运维 2019-09-22 00:01:00 ...

  8. 数据结构 -- 二叉树(Binary Search Tree)

    一.简介 在计算机科学中,二叉树是每个结点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree).二叉树常被用于实现二叉查找树和二叉堆. ...

  9. 将 MathType 公式转换为 Word 自带公式

    以下操作是基于Office 365以及MathType 6.9b平台.有网友留言说第四步没出现「转换为 Office Math」选项,这个我就不清楚了,难道是只有Office 365才支持? 打开Ma ...

  10. ubuntu中不能使用终端的情况

    跟着网上的步骤去升级了一波python3,可谓一波未平! 当我将ubuntu中自带的python3.5升级3.6时,突然发现一个问题,怎么终端打不开了,于是去百度,找到一个博主的笔记,和我的情况一模一 ...