LINUX教程:shell脚本示例:批量比较多个文件的内容是否相同

LINUX教程:shell脚本示例:批量比较多个文件的内容是否相同,第1张

概述介绍《LINUX教程:shell脚本示例批量比较多个文件的内容是否相同》开发教程,希望对您有用。

《liNUX教程:shell脚本示例:批量比较多个文件的内容是否相同》要点:
本文介绍了liNUX教程:shell脚本示例:批量比较多个文件的内容是否相同,希望对您有用。如果有疑问,可以联系我们。

要比拟两个文件的内容是否完全一致,可以简单地使用diff命令.例如:

diff file1 file2 &>/dev/null;echo $?

但是diff命令只能给定两个文件参数,因此无法一次性比拟多个文件(目录也被当作文件),而且diff比拟非文本类文件或者极大的文件时效率极低.

这时可以使用md5sum来实现,相比diff的逐行比拟,md5sum的速度快的多的多.

md5sum的使用办法见:linux中文件MD5校验.

但md5sum只能通过查看md5值来间接比拟文件是否相同,要实现批量自动比拟,则需要写成循环.脚本如下:

#!/bin/bash############################################################  description: compare many files one time               ##  author     : 骏马金龙                                   ##  blog       : http://www.cnblogs.com/f-ck-need-u/       ############################################################# filename: md5.sh# Usage: $0 file1 file2 file3 ...IFS=$'\n'declare -A md5_array# If use while read loop,the array in while statement will# auto set to null after the loop,so i use for statement# instead the while,and so,i modify the variable IFS to# $'\n'.# md5sum format: MD5  /path/to/file# such as:80748c3a55b726226ad51a4bafa1c4aa /etc/fstabfor line in `md5sum "$@"`do    index=${line%% *}    file=${line##* }    md5_array[$index]="$file ${md5_array[$index]}"done# Traverse the md5_arrayfor i in ${!md5_array[@]}do    echo -e "the same file with md5: $i\n--------------\n`echo ${md5_array[$i]}|tr ' ' '\n'`\n"done 

为了测试该脚本,先复制几个文件,并修改此中几个文件的内容,例如:

[root@linuxIDc ~]# for i in `seq -s' ' 6`;do cp -a /etc/fstab /tmp/fs$i;done[root@linuxIDc ~]# echo ha >>/tmp/fs4[root@linuxIDc ~]# echo haha >>/tmp/fs5

现在,/tmp目录下有6个文件fs1、fs2、fs3、fs4、fs5和fs6,此中fs4和fs5被修改,剩余4个文件内容完全相同.

[root@linuxIDc tmp]# ./md5.sh /tmp/fs[1-6]the same file with md5: a612cd5d162e4620b442b0ff3474bf98--------------------------/tmp/fs6/tmp/fs3/tmp/fs2/tmp/fs1the same file with md5: 80748c3a55b726226ad51a4bafa1c4aa--------------------------/tmp/fs4the same file with md5: 30dd43dba10521c1e94267bbd117877b--------------------------/tmp/fs5

更具通用性地比较办法:比较多个目录下的同名文件.

[root@linuxIDc tmp]# find /tmp -type f -name "fs[0-9]" -print0 | xargs -0 ./md5.sh  the same file with md5:a612cd5d162e4620b442b0ff3474bf98--------------------------/tmp/fs6/tmp/fs3/tmp/fs2/tmp/fs1the same file with md5:80748c3a55b726226ad51a4bafa1c4aa--------------------------/tmp/fs4the same file with md5:30dd43dba10521c1e94267bbd117877b--------------------------/tmp/fs5

脚本阐明:

(1).md5sum计算的成果格式为"MD5 /path/to/file",因此要在成果中既输出MD5值,又输出相同MD5对应的文件,考虑使用数组.

(2).一开始的时候我使用while循环,从尺度输入中读取每个文件md5sum的结果.语句如下:

md5sum "$@" | while read index file;do    md5_array[$index]="$file ${md5_array[$index]}"done

但由于管道使得while语句在子shell中执行,于是while中赋值的数组md5_array在循环停止时将失效.所以可改写为:

while read index file;do    md5_array[$index]="$file ${md5_array[$index]}"done <<<"$(md5sum "$@")"

不外我最终还是使用了更繁琐的for循环:

IFS=$'\n'for line in `md5sum "$@"`do    index=${line%% *}    file=${line##* }    md5_array[$index]="$file ${md5_array[$index]}"done

md5sum的每行结果中有两列,而for循环采纳默认的IFS会将这两列分割为两个值,因此还修改了IFS变量的值为$'\n',使得一行赋值一次变量.

(3).index和file变量是为了将md5sum的每一行结果拆分成两个变量,MD5部门作为数组的index,file作为数组变量值的一部门.因此,数组赋值语句为:

md5_array[$index]="$file ${md5_array[$index]}"

(4).数组赋值完成后,开始遍历数组.遍历的办法有多种.我采用的是遍历数组的index列表,即每行的MD5值.

# Traverse the md5_arrayfor i in ${!md5_array[@]}do    echo -e "the same file with md5: $i\n--------------\n`echo ${md5_array[$i]}|tr ' ' '\n'`\n"done  

本文永远更新链接地址

更多liNUX教程,尽在内存溢出PHP学院专栏。欢迎交流《liNUX教程:shell脚本示例:批量比较多个文件的内容是否相同》!

总结

以上是内存溢出为你收集整理的LINUX教程:shell脚本示例:批量比较多个文件的内容是否相同全部内容,希望文章能够帮你解决LINUX教程:shell脚本示例:批量比较多个文件的内容是否相同所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1042187.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存