linux 下文件的比较

1、cmp命令,比较两个文件是否相同

  比较文件test1和test2,如果这两个文件完全相同,则无任何输出,否则,输出第一处不同所在的字节以及行号,然后忽略后面的不同之处,退出命令的执行。

[root@rusky ~]# cat test1
this is a test for comparing files
testA
testB
testC TESTD TEST5
[root@rusky ~]# cat test2
this is a test for comparing files
testA
testB
test3
test4
 
[root@rusky ~]# cmp test1 test2
test1 test2 differ: byte 52, line 4

2、comm 命令,找出两个文件的共同之处

选项:-1不输出第一个文件独有的内容  -2不输出第二个文件独有的内容   -3不输出两个文件独有的内容

[root@rusky ~]# cat test1
this is a test for comparing files
testA
testB
testC TESTD TEST5
You have new mail in /var/spool/mail/root
[root@rusky ~]# cat test2
this is a test for comparing files
testA
testB
test3
test4
 
[root@rusky ~]# comm test1 test2
                this is a test for comparing files
                testA
                testB
        test3
        test4        
testC TESTD TEST5
默认情况下,共显示三列,第1、2列分别表示两个文件各自独有的内容,第3列表示共有的内容。如下例只显示两个文件的共同这处。
[root@rusky ~]# comm -12 test1 test2
this is a test for comparing files
testA
testB 

3、diff和diff3  显示文件的差异 

选项:-i:忽略字母大小写  -w:忽略空格以及制表符  -e:只生成一组指令,以提供给其它程序使用。diff命令除了比较两个文件的差异之处,还会给出一些提示,告诉我们如何修改第一个文件,使得两个文件完全相同。

[root@rusky ~]# diff test1 test2
4c4,6                                          
< testC TESTD TEST5
---              #上部分内容表示将第一个文件第4行的内容修改为第2个文件第4~6行内容;下部分表示用第2个文件的这些内容替换第1个文件的第4行内容。
> test3
> test4
>  
[root@rusky ~]# diff -i test1 test2
4c4,6
< testC TESTD TEST5
---
> test3
> test4
>  

 [root@rusky ~]# diff -e test1 test2
   4c
   test3
   test4

.

diff3命令用来比较三个文件的差异,与diff命令基本相同

  

  

原文地址:https://www.cnblogs.com/rusking/p/3764536.html