码迷,mamicode.com
首页 > 其他好文 > 详细

diff与patch的简单用法

时间:2017-07-31 22:10:09      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:linux   文件比较   

diff

比较相似文件的差异(以行为单位),还可以生成用以更新的“补丁文件”

及指定目录下文件名的不同





diff比较两个目录下文件名的不一致

[root@localhost tmp]# ls aaaa

my.cnf

[root@localhost tmp]# ls bbbb

myy.cnf

[root@localhost tmp]# ls

aaaa  bbbb  my.cnf  myyy.cnf

[root@localhost tmp]# diff aaaa bbbb

Nur in aaaa: my.cnf.

Nur in bbbb: myy.cnf.

运行结果提示出了不同的文件名,安装的vagrant默认不知是哪里的语言,还没修改过来,了解意思就好。。。




[root@localhost tmp]# cat -n my.cnf

     1  [mysqld]

     2  datadir=/var/lib/mysql

     3  socket=/var/lib/mysql/mysql.sock

     4  # Disabling symbolic-links is recommended to prevent assorted security risks

     5  symbolic-links=0

     6  # Settings user and group are ignored when systemd is used.

     7  # If you need to run mysqld under a different user or group,

     8  # customize your systemd unit file for mariadb according to the

     9  # instructions in http://fedoraproject.org/wiki/Systemd

    10

    11  [mysqld_safe]

    12  log-error=/var/log/mariadb/mariadb.log

    13  pid-file=/var/run/mariadb/mariadb.pid

    14

    15  #

    16  # include all files from the config directory

    17  #

    18  !includedir /etc/my.cnf.d

    19

[root@localhost tmp]# cp my.cnf  mmmm.cnf

[root@localhost tmp]# diff my.cnf  mmmm.cnf

相同内容的文件,运行diff比较之后没有输出。

[root@localhost tmp]# cat my.cnf | sed -e ‘4d‘ -e ‘6c this line was replaced!‘ >myyy.cnf

[root@localhost tmp]# cat -n myyy.cnf

     1  [mysqld]

     2  datadir=/var/lib/mysql

     3  socket=/var/lib/mysql/mysql.sock

     4  symbolic-links=0

     5  this line was replaced!

     6  # If you need to run mysqld under a different user or group,

     7  # customize your systemd unit file for mariadb according to the

     8  # instructions in http://fedoraproject.org/wiki/Systemd

     9

    10  [mysqld_safe]

    11  log-error=/var/log/mariadb/mariadb.log

    12  pid-file=/var/run/mariadb/mariadb.pid

    13

    14  #

    15  # include all files from the config directory

    16  #

    17  !includedir /etc/my.cnf.d

    18

制造不同,并另存为新文件myyy.cnf

[root@localhost tmp]# diff my.cnf myyy.cnf

4d3

< # Disabling symbolic-links is recommended to prevent assorted security risks

6c5

< # Settings user and group are ignored when systemd is used.

---

> this line was replaced!

仔细理解下diff的输出:它并不执着于两个文件内容对应的行号是否完全一致。<号后对应的是命令中左侧文件的不一致部分,可以理解为箭头指向左侧,相应>号后是命令中右侧文件的不一致部分。d表示删掉,4d3表示定位到左侧文件第四行内容在右侧文件中被删除,右侧文件定位到第三行后。6c5则表示左侧文件的第六行被右侧文件第五行替换,之后分别列出了相应内容。也可以理解为,左侧文件经过在第几行处的d 、 c操作即可得出右侧新文件



下面再制造一个不同之处多些的文件

[root@localhost tmp]# cat my.cnf  | sed -e ‘4d‘ -e ‘6c this line was replaced!‘ -e ‘9i this is insert part‘ > new.cnf

[root@localhost tmp]# sed -i ‘12d‘ new.cnf

[root@localhost tmp]# cat new.cnf | grep symbolic

symbolic-links=0

[root@localhost tmp]# cat new.cnf | grep symbolic > 111

[root@localhost tmp]# cat 111

symbolic-links=0

[root@localhost tmp]# sed -i ‘4d‘ new.cnf ; cat 111 >>new.cnf

[root@localhost tmp]# cat -n my.cnf

     1  [mysqld]

     2  datadir=/var/lib/mysql

     3  socket=/var/lib/mysql/mysql.sock

     4  # Disabling symbolic-links is recommended to prevent assorted security risks

     5  symbolic-links=0

     6  # Settings user and group are ignored when systemd is used.

     7  # If you need to run mysqld under a different user or group,

     8  # customize your systemd unit file for mariadb according to the

     9  # instructions in http://fedoraproject.org/wiki/Systemd

    10

    11  [mysqld_safe]

    12  log-error=/var/log/mariadb/mariadb.log

    13  pid-file=/var/run/mariadb/mariadb.pid

    14

    15  #

    16  # include all files from the config directory

    17  #

    18  !includedir /etc/my.cnf.d

    19

[root@localhost tmp]# cat -n new.cnf

     1  [mysqld]

     2  datadir=/var/lib/mysql

     3  socket=/var/lib/mysql/mysql.sock

     4  this line was replaced!

     5  # If you need to run mysqld under a different user or group,

     6  # customize your systemd unit file for mariadb according to the

     7  this is insert part

     8  # instructions in http://fedoraproject.org/wiki/Systemd

     9

    10  [mysqld_safe]

    11  pid-file=/var/run/mariadb/mariadb.pid

    12

    13  #

    14  # include all files from the config directory

    15  #

    16  !includedir /etc/my.cnf.d

    17

    18  symbolic-links=0

[root@localhost tmp]# diff my.cnf  new.cnf

4,6c4

< # Disabling symbolic-links is recommended to prevent assorted security risks

< symbolic-links=0

< # Settings user and group are ignored when systemd is used.

---

> this line was replaced!

8a7

> this is insert part

12d10

< log-error=/var/log/mariadb/mariadb.log

19a18

> symbolic-links=0

这样似乎理解得更全面些。结合内容理解一下diff的运行结果。注意4,6是指左侧文件的第四五六行(四到六),而不是第四和六。


[root@localhost tmp]# diff -u my.cnf new.cnf > 222

[root@localhost tmp]# cat 222

--- my.cnf      2017-07-31 05:23:33.351268214 +0200

+++ new.cnf     2017-07-31 07:19:28.378359268 +0200

@@ -1,15 +1,13 @@

 [mysqld]

 datadir=/var/lib/mysql

 socket=/var/lib/mysql/mysql.sock

-# Disabling symbolic-links is recommended to prevent assorted security risks

-symbolic-links=0

-# Settings user and group are ignored when systemd is used.

+this line was replaced!

 # If you need to run mysqld under a different user or group,

 # customize your systemd unit file for mariadb according to the

+this is insert part

 # instructions in http://fedoraproject.org/wiki/Systemd


 [mysqld_safe]

-log-error=/var/log/mariadb/mariadb.log

 pid-file=/var/run/mariadb/mariadb.pid


 #

@@ -17,3 +15,4 @@

 #

 !includedir /etc/my.cnf.d


+symbolic-links=0

显示出公共行,方便比对。其中-后内容为左侧文件数据,+后为右侧文件数据







patch

根据diff -u file1 file2 命令运行结果更新file1内容与file2一致



[root@localhost tmp]# patch  -p0 < 222

patching file my.cnf

[root@localhost tmp]# diff my.cnf new.cnf

[root@localhost tmp]# patch  -R -p0 < 222

patching file my.cnf

[root@localhost tmp]# diff my.cnf new.cnf

4,6c4

< # Disabling symbolic-links is recommended to prevent assorted security risks

< symbolic-links=0

< # Settings user and group are ignored when systemd is used.

---

> this line was replaced!

8a7

> this is insert part

12d10

< log-error=/var/log/mariadb/mariadb.log

19a18

> symbolic-links=0

-p0取消目录层级。-R是反向的意思,还原更新。所以新旧文件顺序,在用diff生成patch文件时就很重要,一定习惯 旧文件 新文件 这样的比对顺序。





待续。。。





diff与patch的简单用法

标签:linux   文件比较   

原文地址:http://lybliangliang.blog.51cto.com/3808302/1952360

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!