现在加入有很多很多文件需要测试md5,想看下是否都传输成功了,如何批量生成文件的md5并且逐条对照呢?
下面来简单介绍下
md5sum这个命令有一个选项“-c”
这个选项的意思是check,检查对照目标文件的md5值是否和源文件生成的md5是否相同
下面举一个实例:
192.168.1.11有一个test文件夹,里面存放了大大小小很多的文件,文件夹,现在想将这个文件传到192.168.1.12上
文件已经传过去了,但是后台的日志报错了,想看下是否都传输成功
find test/ -type f -print0 | xargs -0 md5sum > check.md5
主要说下-print0 和 -0
-print 0 解释:
True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.
大体意思是:会输出完整的文件名称,并以一个空字符为分割,而不是换行,要用特殊的命令来处理输出结果,最直接的就是xargs -0
xargs -0的解释:
Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
大体意思是:输出项由空字符终止,而不是空格,或者换行. 禁用文件字符串的结尾,该字符串与任何其他参数一样。 输入项可能包含空格,引号或反斜杠时很有用。 GNU find -print0选项生成适合此模式的输入。
所以find -print0 和xargs和 -0 是一对
生成的check.md5传输到目的地址,注意目录的位置要和源地址的一样,如果源地址是/home/test/下生成的check.md5,那么目的地址也就是/home/test/下对照md5,这里容易出错
执行如下语句,检查文件md5是否相同
md5sum -c check.md5 > complete.log
文件执行成功后,会提示有几个不匹配
md5sum: WARNING: 3 computed checksums did NOT match
这里提示就有三个有问题的,不匹配的直接grep就可以
grep FAILED complete.log
就知道那个文件出现问题了!