范例: 1)、比较两文件的差异 $diff a.txt b.txt 2c2 这两个文件在第2行有所不同 < line 2 : differences a.txt 第2行的内容 ---------------------- > line 2 b.txt 第2行的内容 2)、比较两个文件的差异,只列出有无差异,并不显示详细信息 $diff -q a.txt b.txt Files a.txt and b.txt differ 只指出两个文件有所不同 3)、比较两个文件,将两个文件的全部内容分别显示在左右两侧 $diff -y a.txt b.txt line 1 line 1 line 2 : differences | line 2 a.txt的文件内容 b.txt的文件内容 4)、比较两个文件,仅在左侧显示相同内容,在两侧显示不同内容 $diff -y --left-column a.txt b.txt line1 line2 line3 : differences | line 3 5)、比较两个文件,只将不同的行显示在左右两侧 $diff -y --suppress-common-line a.txt b.txt line 2 :differences | line 2 6)、显两文件不同处,同时显示相异处前后内容 $diff -c a.txt b.txt 7)、比较a.txt与b.txt两个文件,显示相异处前后各两行的内容,并标出这两个文件的不同处 $diff -c -2 a.txt b.txt 8)、比较a.txt与b.txt两个文件,显示相异处前后部分的内容,并将结果以合并的方式列出 $diff -u a.txt b.txt 9)、比较a.txt与b.txt两个文件的不同,并显示相异处前后各行的内容,并将结果以合并的方式列出 $diff -U 2 a.txt b.txt 10)、比较dir1与dir2两个目录中文件的异同 $diff dir1 dir2 11)、忽略内容中大小写的差异 $diff -is dir1 dir2 12)、比较dir1与dir2目录中的文件时,若文件中包含有question字符串,则忽略不比较此行 $diff -I question -s dir1 dir2 13)、比较dir1与dir2目录中的文件,并以并列方式显示文件的相异之处 $diff -y --suppress-common-lines dir1 dir2 14)、比较dirA与dirB两个目录下文件的异同,比较时连同子目录下的文件一并比较 $diff -r dirA dirB 15)、比较dirA与dirB两个目录下文件的异同,比较时忽略名称为file1的文件 $diff -x file1 dirA dirB 16)、比较a.txt与b.txt两个文件的异同,但比较时不比较空格数目的不同 $diff -b a.txt b.txt 17)、比较a.txt与b.txt两个文件的异同,比较时忽略空行 $diff -B a.txt b.txt 18)、比较二进制文件 $diff -a a.txt b.txt 19)、a.c与b.c是C语言的程序代码文件,比较两者异同,不一样的地方,列出差异所在函数 $diff -p a.c b.c 20)、比较两个文件并忽略空白字符和空白行,且当文件相同时仅显示左侧文件内容 $diff -bBy --left-column file1 file2 |
两个文件: |