WenJunjie Blog

sysadmin

Linux Compare Files

| Comments

Linux Compare Files

  • 目录比较 diff -r -brief file1 file2 -brief不显示差异的细节

linux 快速比较两个目录 diff -c -r service service.bak diff -c -a -b -r service service.bak

diff -r service service.bak diff -r –brief service service.bak

diff -qr service service.bak rsync -vnr service/ service.bak/

-n, –dry-run perform a trial run with no changes made rsync -avn src dst –delete –size-only –ignore-existing

可以用: diff -qr directory1/ directory2/ rsync -vnr directory1/ directory2/ 但是很慢。因为文件也被比较了。给rsync加上“–ignore-existing”的选项要好很多。 如果只是想比较目录结构,可以 ls -R directory1 > directory1.txt ls -R directory2 > directory2.txt diff –side-by-side –suppress-common-lines directory1.txt directory2.txt 如果给ls加上“-1s”的选项(注意:是1,2,3的1,不是l,m,n的l),还可以比较文件的大小。

假如我要比较目录A和目录B,使用下面的命令 diff -c -a -b -x .bak -x .log A B > AB.diff gvim AB.diff 这样就可以方便的查看不同了,目前我的vim的diff配色比较舒适。 解释一下参数的意义 -c 采用content的format输出different -a 强制使用text的模式 -b 忽略空白的不同 -x 过滤掉所比较目录中一些不想比较的文件类型,可以使用其他的pattern 实际上如果需要过滤的文件类型比较多的时候,使用-x这个选项就有点麻烦了,查看了文档之后,diff提供了更加方便的参数过滤文件。 -X excludefile 忽略在excludefile中的文件类型,注意每种文件占一行

对比两个文件 先过滤掉重复的行 cat 1.txt |sort |uniq -c|sort -rn|awk ‘{print $2}’ >file1.txt cat 2.txt |sort |uniq -c|sort -rn|awk ‘{print $2}’ >file2.txt cat file1.txt |xargs -l1 -i grep {} file2.txt > tmp.txt

echo -n ‘hello’|md5sum|cut -d ‘ ’ -f1 md5sum log.iso |cut -d ‘ ’ -f1 md5sum log.iso > log.iso.md5 md5sum -c log.iso.md5

compare files ignoring white diff -w file1.txt file2.txt

compare files side by side, ingoring white space diff -by file1.txt file2.txt

compare files ignoring case diff -iy file1.txt file2.txt

Comments