现有text1.txt和text2.txt两个文件: text1.txt
this is the first line this is the second line this is the thiid linetext2.txt
this is the first line this is the second line this is the third line两个文件的区别是第三行 third 单词有误。
使用diff命令输出两个文件的区别:
diff text1.txt text2.txtoutput:
3c3 < this is thiid line --- > this is third line将diff输出保存为patch文件:
diff text1.txt text2.txt > amend.patch拿到amend.patch文件后,可以使用patch命令将其作用在text1.txt 或者 text2.txt
amend.patch文件保存的是由text1.txt到text2.txt的修改,所以将amend.patch作用到text1.txt文件上,text1.txt就会变为text2.txt
patch text1.txt amend.patch这时参看text1.txt:
cat text1.txtoutput:
this is first line this is second line this is third linetext1.txt的单词错误已经改正,与文件text2.txt文件一致。
amend.patch文件同样可以作用到text2.txt上,不过patch命令会探测到由text2.txt回退到text1.txt,这时需要加上-R参数:
patch -R text2.txt amend.patch