内容简介:diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方。diff在命令行中打印每一个行的改动。最新版本的diff还支持二进制文件。
diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方。diff在命令行中打印每一个行的改动。最新版本的diff还支持二进制文件。diff程序的输出被称为补丁 (patch),因为 Linux 系统中还有一个patch程序,可以根据diff的输出将a.c的文件内容更新为b.c。diff是svn、cvs、git等版本控制 工具 不可或缺的一部分。
1.命令格式:
|
1 |
diff[参数][文件1或目录1][文件2或目录2] |
2.命令功能:
diff命令能比较单个文件或者目录内容。如果指定比较的是文件,则只有当输入为文本文件时才有效。以逐行的方式,比较文本文件的异同处。如果指定比较的是目录的的时候,diff 命令会比较两个目录下名字相同的文本文件。列出不同的二进制文件、公共子目录和只在一个目录出现的文件。
3.命令参数:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
- 指定要显示多少行的文本。此参数必须与-c或-u参数一并使用。 -a或--text diff预设只会逐行比较文本文件。 -b或--ignore-space-change 不检查空格字符的不同。 -B或--ignore-blank-lines 不检查空白行。 -c 显示全部内文,并标出不同之处。 -C或--context 与执行"-c-"指令相同。 -d或--minimal 使用不同的演算法,以较小的单位来做比较。 -D或ifdef 此参数的输出格式可用于前置处理器巨集。 -e或--ed 此参数的输出格式可用于ed的script文件。 -f或-forward-ed 输出的格式类似ed的script文件,但按照原来文件的顺序来显示不同处。 -H或--speed-large-files 比较大文件时,可加快速度。 -l或--ignore-matching-lines 若两个文件在某几行有所不同,而这几行同时都包含了选项中指定的字符或字符串,则不显示这两个文件的差异。 -i或--ignore-case 不检查大小写的不同。 -l或--paginate 将结果交由pr程序来分页。 -n或--rcs 将比较结果以RCS的格式来显示。 -N或--new-file 在比较目录时,若文件A仅出现在某个目录中,预设会显示:Only in目录:文件A若使用-N参数,则diff会将文件A与一个空白的文件比较。 -p 若比较的文件为C语言的程序码文件时,显示差异所在的函数名称。 -P或--unidirectional-new-file 与-N类似,但只有当第二个目录包含了一个第一个目录所没有的文件时,才会将这个文件与空白的文件做比较。 -q或--brief 仅显示有无差异,不显示详细的信息。 -r或--recursive 比较子目录中的文件。 -s或--report-identical-files 若没有发现任何差异,仍然显示信息。 -S或--starting-file 在比较目录时,从指定的文件开始比较。 -t或--expand-tabs 在输出时,将tab字符展开。 -T或--initial-tab 在每行前面加上tab字符以便对齐。 -u,-U或--unified= 以合并的方式来显示文件内容的不同。 -v或--version 显示版本信息。 -w或--ignore-all-space 忽略全部的空格字符。 -W或--width 在使用-y参数时,指定栏宽。 -x或--exclude 不比较选项中所指定的文件或目录。 -X或--exclude-from 您可以将文件或目录类型存成文本文件,然后在=中指定此文本文件。 -y或--side-by-side 以并列的方式显示文件的异同之处。 --help 显示帮助。 --left-column 在使用-y参数时,若两个文件某一行内容相同,则仅在左侧的栏位显示该行内容。 --suppress-common-lines 在使用-y参数时,仅显示不同之处。 |
4.使用实例:
实例1:比较两个文件
命令:
输出:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@localhost test3]# diff log2014.log log2013.log 3c3 < 2014-03 --- > 2013-03 8c8 < 2013-07 --- > 2013-08 11,12d10 < 2013-11 < 2013-12 |
说明:
上面的“3c3”和“8c8”表示log2014.log和log20143log文件在3行和第8行内容有所不同;”11,12d10″表示第一个文件比第二个文件多了第11和12行。
diff 的normal 显示格式有三种提示:
|
1 2 3 4 5 |
a - add c - change d - delete |
实例2:并排格式输出
命令:
|
1 |
diff log2013.log log2014.log -y -W 50 |
输出:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
[root@localhost test3]# diff log2014.log log2013.log -y -W 50 2013-01 2013-01 2013-02 2013-02 2014-03 | 2013-03 2013-04 2013-04 2013-05 2013-05 2013-06 2013-06 2013-07 2013-07 2013-07 | 2013-08 2013-09 2013-09 2013-10 2013-10 2013-11 < 2013-12 < [root@localhost test3]# diff log2013.log log2014.log -y -W 50 2013-01 2013-01 2013-02 2013-02 2013-03 | 2014-03 2013-04 2013-04 2013-05 2013-05 2013-06 2013-06 2013-07 2013-07 2013-08 | 2013-07 2013-09 2013-09 2013-10 2013-10 > 2013-11 > 2013-12 |
说明:
|
1 2 3 4 5 |
“|”表示前后2个文件内容有不同 “<”表示后面文件比前面文件少了1行内容 “>”表示后面文件比前面文件多了1行内容 |
实例3:上下文输出格式
命令:
|
1 |
diff log2013.log log2014.log -c |
输出:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
[root@localhost test3]# diff log2013.log log2014.log -c *** log2013.log 2012-12-07 16:36:26.000000000 +0800 --- log2014.log 2012-12-07 18:01:54.000000000 +0800 *************** *** 1,10 **** 2013-01 2013-02 ! 2013-03 2013-04 2013-05 2013-06 2013-07 ! 2013-08 2013-09 2013-10 --- 1,12 ---- 2013-01 2013-02 ! 2014-03 2013-04 2013-05 2013-06 2013-07 ! 2013-07 2013-09 2013-10 + 2013-11 + 2013-12[root@localhost test3]# diff log2014.log log2013.log -c *** log2014.log 2012-12-07 18:01:54.000000000 +0800 --- log2013.log 2012-12-07 16:36:26.000000000 +0800 *************** *** 1,12 **** 2013-01 2013-02 ! 2014-03 2013-04 2013-05 2013-06 2013-07 ! 2013-07 2013-09 2013-10 - 2013-11 - 2013-12 --- 1,10 ---- 2013-01 2013-02 ! 2013-03 2013-04 2013-05 2013-06 2013-07 ! 2013-08 2013-09 2013-10[root@localhost test3]# |
说明:
这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:
|
1 2 3 4 5 |
“+” 比较的文件的后者比前着多一行 “-” 比较的文件的后者比前着少一行 “!” 比较的文件两者有差别的行 |
实例4:统一格式输出
命令:
|
1 |
diff log2014.log log2013.log -u |
输出:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
[root@localhost test3]# diff log2014.log log2013.log -u --- log2014.log 2012-12-07 18:01:54.000000000 +0800 +++ log2013.log 2012-12-07 16:36:26.000000000 +0800 @@ -1,12 +1,10 @@ 2013-01 2013-02 -2014-03 +2013-03 2013-04 2013-05 2013-06 2013-07 -2013-07 +2013-08 2013-09 2013-10 -2013-11 -2013-12 |
说明:
它的第一部分,也是文件的基本信息:
|
1 2 3 |
--- log2014.log 2012-12-07 18:01:54.000000000 +0800 +++ log2013.log 2012-12-07 16:36:26.000000000 +0800 |
“—“表示变动前的文件,”+++”表示变动后的文件。
第二部分,变动的位置用两个@作为起首和结束。
|
1 |
@@ -1,12 +1,10 @@ |
前面的”-1,12″分成三个部分:减号表示第一个文件(即log2014.log),”1″表示第1行,”12″表示连续12行。合在一起,就表示下面是第一个文件从第1行开始的连续12行。同样的,”+1,10″表示变动后,成为第二个文件从第1行开始的连续10行。
实例5:比较文件夹不同
命令:
|
1 |
diff test3 test6 |
输出:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
[root@localhost test]# diff test3 test6 Only in test6: linklog.log Only in test6: log2012.log diff test3/log2013.log test6/log2013.log 1,10c1,3 < 2013-01 < 2013-02 < 2013-03 < 2013-04 < 2013-05 < 2013-06 < 2013-07 < 2013-08 < 2013-09 < 2013-10 --- > hostnamebaidu=baidu.com > hostnamesina=sina.com > hostnames=true diff test3/log2014.log test6/log2014.log 1,12d0 < 2013-01 < 2013-02 < 2014-03 < 2013-04 < 2013-05 < 2013-06 < 2013-07 < 2013-07 < 2013-09 < 2013-10 < 2013-11 < 2013-12 Only in test6: log2015.log Only in test6: log2016.log Only in test6: log2017.log [root@localhost test]# |
说明:
实例6:比较两个文件不同,并生产补丁
命令:
|
1 |
diff -ruN log2013.log log2014.log >patch.log |
输出:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
[root@localhost test3]# diff -ruN log2013.log log2014.log >patch.log [root@localhost test3]# ll 总计 12 -rw-r--r-- 2 root root 80 12-07 16:36 log2013.log -rw-r--r-- 1 root root 96 12-07 18:01 log2014.log -rw-r--r-- 1 root root 248 12-07 21:33 patch.log [root@localhost test3]# cat patc.log cat: patc.log: 没有那个文件或目录 [root@localhost test3]# cat patch.log --- log2013.log 2012-12-07 16:36:26.000000000 +0800 +++ log2014.log 2012-12-07 18:01:54.000000000 +0800 @@ -1,10 +1,12 @@ 2013-01 2013-02 -2013-03 +2014-03 2013-04 2013-05 2013-06 2013-07 -2013-08 +2013-07 2013-09 2013-10 +2013-11 +2013-12[root@localhost test3]# |
说明:
实例7:打补丁
命令:
输出:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
[root@localhost test3]# cat log2013.log 2013-01 2013-02 2013-03 2013-04 2013-05 2013-06 2013-07 2013-08 2013-09 2013-10[root@localhost test3]# patch log2013.log patch.log patching file log2013.log [root@localhost test3]# [root@localhost test3]# cat log2013.log 2013-01 2013-02 2014-03 2013-04 2013-05 2013-06 2013-07 2013-07 2013-09 2013-10 2013-11 2013-12[root@localhost test3]# |
说明:
本系列文章:
每天一个 Linux 命令(1):ls命令
每天一个 Linux 命令(2):cd命令
每天一个 Linux 命令(3):pwd命令
每天一个 Linux 命令(4):mkdir命令
每天一个 Linux 命令(5):rm 命令
每天一个 Linux 命令(6):rmdir 命令
每天一个 Linux 命令(7):mv命令
每天一个 Linux 命令(8):cp 命令
每天一个 Linux 命令(9):touch 命令
每天一个 Linux 命令(10):cat 命令
每天一个 Linux 命令(11):nl 命令
每天一个 Linux 命令(12):more 命令
每天一个 Linux 命令(13):less 命令
每天一个 Linux 命令(14):head 命令
每天一个 Linux 命令(15):tail 命令
每天一个 Linux 命令(16):which命令
每天一个 Linux 命令(17):whereis 命令
每天一个 Linux 命令(18):locate 命令
每天一个 Linux 命令(19):find 命令概览
每天一个 Linux 命令(20):find命令之exec
每天一个 Linux 命令(21):find命令之xargs
每天一个 Linux 命令(22):find 命令的参数详解
每天一个 Linux 命令(23):Linux 目录结构
每天一个 Linux 命令(24):Linux 文件类型与扩展名
每天一个 Linux 命令(25):Linux 文件属性详解
每天一个 Linux 命令(26):用 SecureCRT 来上传和下载文件
每天一个 Linux 命令(27):linux chmod 命令
每天一个 Linux 命令(28):tar 命令
每天一个 Linux 命令(29): chgrp 命令
每天一个 Linux 命令(30): chown 命令
每天一个 Linux 命令(31): /etc/group 文件详解
每天一个 Linux 命令(32):gzip 命令
每天一个 Linux 命令(33):df 命令
每天一个 Linux 命令(34): du 命令
每天一个 Linux 命令(35): ln 命令
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- composer更新命令及常用命令
- Linux命令行与命令
- AWK命令和SED命令
- 每天一个 Linux 命令(60): scp命令
- 每天一个 Linux 命令(59): rcp 命令
- 每天一个 Linux 命令(58): telnet 命令
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Designing Data-Intensive Applications
Martin Kleppmann / O'Reilly Media / 2017-4-2 / USD 44.99
Data is at the center of many challenges in system design today. Difficult issues need to be figured out, such as scalability, consistency, reliability, efficiency, and maintainability. In addition, w......一起来看看 《Designing Data-Intensive Applications》 这本书的介绍吧!