diff以行为单位比较不一样ASCII文件差别,能够输出一组指令,用于指导如何更改一个文件使其与第二个文件相同。diff在软件开发时多用于比较新旧版本代码,和patch连用能够将文件间区别作成补丁。linux
参考:Beginner's Guide to Installing from Source Patching一节promise
diff [-bBi] from-file to-filecookie
from-file :源文件
to-file :目标文件app
-b :忽略一行当中,仅有多个空白的差别(例如 "about me" 与 "about me" 视为相同
-B :忽略空白行的差别。
-i :忽略大小写的不一样。ide
diff -Naur passwd.old passwd.new > passwd.patchui
补丁文件通常都以.patch结尾spa
patch -pN < patch_file
.net
patch -R -pN < patch_file
code
选项与参数:blog
-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 2,4c2,4 < 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 2018-06-15 03:42:16.841974345 -0400 --- file2.txt 2018-06-15 03:42:45.919587320 -0400 *************** *** 1,4 **** apples - oranges kiwis carrots --- 1,4 ---- apples kiwis carrots + grapefruits
前两行:***标识源文件,---标识目标文件,而后又分别跟文件名,修改日期时间
"***************" 仅仅是分隔符方便阅读
***第一个文件行的范围****
第一个文件每行的内容。若是这行没变化,则以2个开头。若是这一行变化了,则一个标识字符+开头
标识字符以下:
---第二个文件行的范围----
[root@localhost ~]# diff -u file1.txt file2.txt --- file1.txt 2018-06-15 03:42:16.841974345 -0400 +++ file2.txt 2018-06-15 03:42:45.919587320 -0400 @@ -1,4 +1,4 @@ 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 5,6c She would look up into the clouds and dream of being a world-famous baker. . 2,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 5,6c She would look up into the clouds and dream of being a world-famous baker. . 2,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- <