稍微简化的版本,避免多次读取同一文件(bash 4.0 及更高版本)。我假设文件包含唯一的文件名,文件格式是md5sum命令的输出。
#!/bin/bash
declare -A hash
while read md5 file; do hash[$file]=$md5; done <chksum
while read md5 file; do
[ -z "${hash[$file]}" ] && echo "$file new file" && continue
[ ${hash[$file]} == $md5 ] && echo "$file is same" && continue
echo "$file has been changed"
done <chksum1
此脚本将第一个文件读取到关联数组,称为hash
. index 是文件名,value 是 MD5 校验和。第二个循环读取第二个校验和文件;文件名不在hash
打印中file new file
;如果它在hash
并且值等于,那么它是同一个文件;如果它不等于它写file has been changed
。
输入文件:
$ cat chksum
eed0fc0313f790cec0695914f1847bca ./a.txt
9ee9e1fffbb3c16357bf80c6f7a27574 ./b.txt
a91a408e113adce865cba3c580add827 ./c.txt
$ cat chksum1
eed0fc0313f790cec0695914f1847bca ./a.txt
8ee9e1fffbb3c16357bf80c6f7a27574 ./b.txt
a91a408e113adce865cba3c580add827 ./d.txt
输出:
./a.txt is same
./b.txt has been changed
./d.txt new file
扩大的视野
还检测已删除的文件。
#!/bin/bash
declare -A hash
while read md5 file; do hash[$file]=$md5; done <chksum
while read md5 file; do
[ -z "${hash[$file]}" ] && echo "$file new file" && continue
if [ ${hash[$file]} == $md5 ]; then echo "$file is same"
else echo "$file has been changed"
fi
unset hash[$file]
done <chksum1
for file in ${!hash[*]};{ echo "$file deleted file";}
输出:
./a.txt is same
./b.txt has been changed
./d.txt new file
./c.txt deleted file