Shell编程中的常用命令diff和patch

Viridis ·
更新时间:2024-11-10
· 553 次阅读

Shell编程中的常用命令diff和patch

diff和patch命令

diff通常比较文件的内容(产生补丁文件),patch常用来打补丁

<表示第一个文件中的内容

>表示第二个文件中的内容

 

diff -u

diff -r     #比较目录

diff通常比较目录的时候 比较的结果是目录中文件的不同,不能比较文件内容的不同

(有没有相同名字的文件)

a表示添加 ----add

c表示更改 ----change

d表示删除 ----delete

 

Diff file1 file2   #以第二个文件为基准,看第一个文件跟第二个文件的区别(第一个如何能变成第二个文件)

 

[root@localhost diff_patch]# cat file1

123

haha

[root@localhost diff_patch]# cat file2

123

nihao

[root@localhost diff_patch]# diff file1 file2

2c2            

#第一个2是第一个文件的第二行   c:动作更改   第二个2:第二个问文件的第二行

#表示第一个文件中的第二行改变了后可以变为第二行

< haha         #第一个文件的第二行的内容

---

> nihao        

#第二个文件的第二行的内容(第一个文件的第二行内容将要变成的东西)

 

[root@localhost diff_patch]# diff file2 file1    file2如何才能变成file1

2c2

< nihao

---

> haha

 

 

[root@localhost diff_patch]# cat file1

123

[root@localhost diff_patch]# cat file2

123

nihao

[root@localhost diff_patch]# diff file1 file2

1a2          #第一个文件的第一行

> nihao      #将第二文件的第二行内容加到第一个文件第一行上

 

 

[root@localhost diff_patch]# diff file2 file1

2d1       #第一个文件的第二删除第一个文件的内容nihao,就得到第二个文件了

< nihao

 

 

 

diff -u:-u表示产生补丁,  以第二个文件为标准(最终变成的文件)

diff -u file1 file2

 

[root@localhost diff_patch]# diff -u file1 file2

--- file1    2020-02-15 10:55:10.502000000 +0800

+++ file2  2020-02-15 10:44:02.246000000 +0800

@@ -1 +1,2 @@

 123

+nihao

 

diff -u file1 file2 > file2.patch

 

[root@localhost diff_patch]# diff -u file1 file2 > file2.patch

[root@localhost diff_patch]# cat file2.patch

--- file1    2020-02-15 10:55:10.502000000 +0800

+++ file2  2020-02-15 10:44:02.246000000 +0800

@@ -1 +1,2 @@

 123

+nihao

 

[root@localhost diff_patch]# patch file2 file2.patch

bash: patch: command not found...

#发现没有装patch命令

[root@localhost yum.repos.d]# yum clean all

 

[root@localhost yum.repos.d]# yum repolist

[root@localhost yum.repos.d]# yum whatprovides */patch

……

Filename    : /usr/src/kernels/3.10.0-514.el7.x86_64/include/config/snd/hda/patch

 

 

 

patch-2.7.1-8.el7.x86_64 : Utility for modifying/upgrading files

Repo        : test

Matched from:

Filename    : /usr/bin/patch

 

 

[root@localhost yum.repos.d]# yum install -y patch-2.7.1-8.el7.x86_64

 

[root@localhost diff_patch]# diff -u file1 file2 > file1.patch

[root@localhost diff_patch]# ls

file1  file1.patch  file2  file2.patch  hui.dudu

[root@localhost diff_patch]# rm -fr file2.patch

[root@localhost diff_patch]# ls

file1  file1.patch  file2  hui.dudu

 

patch file1 file1.patc

[root@localhost diff_patch]# patch file1 file1.patch     #不保留原文件(内容)

#这样打补丁原文件就没有了(不推荐)

#给file1打补丁,来和file2一样

patching file file1

[root@localhost diff_patch]# diff file1 file2

[root@localhost diff_patch]# cat file1

123

nihao

[root@localhost diff_patch]# cat file2

123

nihao

[root@localhost diff_patch]#

 

 

 

 

[root@localhost diff_patch]# cat file1

123

nihao

[root@localhost diff_patch]# cat file3

123

Haha

[root@localhost diff_patch]# diff -u file3 file1 

--- file3    2020-02-15 11:50:58.543000000 +0800

+++ file1  2020-02-15 11:44:35.921000000 +0800

@@ -1,2 +1,2 @@

 123

-haha

+nihao

产生file3的补丁

[root@localhost diff_patch]# diff -u file3 file1 > file3.patch

[root@localhost diff_patch]# ls

file1  file1.patch  file2  file3  file3.patch  hui.dudu

 

patch -b file3 file3.patch   

(保留原文件内容,打完补丁后,生成.orig文件(内容与原文件相同))

 

[root@localhost diff_patch]# patch -b file3 file3.patch

patching file file3

[root@localhost diff_patch]# ls

file1  file1.patch  file2  file3  file3.orig  file3.patch  hui.dudu

[root@localhost diff_patch]# cat file3.orig

123

haha

 

diff -r比较目录

[root@localhost diff_patch]# diff /etc/ /tmp/

Only in /etc/: abrt

Only in /etc/: adjtime

Only in /etc/: aliases

Only in /etc/: aliases.db


作者:huige永生



diff patch shell编程 shell

需要 登录 后方可回复, 如果你还没有账号请 注册新账号