swa*_*doc 6 command-line md5sum text-processing
我有两个文件MD1和MD2.
MD1 包含 md5sums:
5f31caf675f2542a971582442a6625f6 /root/md5filescreator/hash1.txt
4efe4ba4ba9fd45a29a57893906dcd30 /root/md5filescreator/hash2.txt
1364cdba38ec62d7b711319ff60dea01 /root/md5filescreator/hash3.txt
Run Code Online (Sandbox Code Playgroud)
其中hash1,hash2和hash3是文件夹中存在的三个文件md5filescreator。
同样MD2包含:
163559001ec29c4bbbbe96344373760a /root/md5filescreators/hash1.txt
4efe4ba4ba9fd45a29a57893906dcd30 /root/md5filescreators/hash2.txt
1364cdba38ec62d7b711319ff60dea01 /root/md5filescreators/hash3.txt
Run Code Online (Sandbox Code Playgroud)
这些文件在文件夹中的位置md5filescreators。
我想将校验和md5filescreator与相应文件的校验和进行比较md5filecreators。
对于校验和相同的文件,shell 脚本应该返回OK,对于校验和不同的文件以及文件名,shell 脚本应该返回FALSE。
可以使用md5sum --check(因为它通常只检查 1 个 MD5 文件中的任何更改)来完成吗?
我想知道这是否可以使用
md5sum --check? (因为它通常只检查 1 个 MD5 文件中的任何更改)。
不,不能。
md5sum --check用于读取输入文件第二列中每个文件的路径,并根据第一列报告的校验和再次检查它们的 MD5 校验和;如果要直接比较两个文件中的校验和,则必须比较文本文件。
使用paste+ AWK 你可以:
paste file1 file2 | awk '{x = $1 == $3 ? "OK" : "FALSE"; print $2" "x}'
Run Code Online (Sandbox Code Playgroud)
paste file1 file2: 连接第 Nfile1行的第 N 行file2;awk '{x = $1 == $3 ? "OK" : "FALSE"; print $2" "x}': 如果第一个字段等于第三个字段(即 MD5 和匹配),则将“OK”x分配给,否则将“FALSE”分配给x并打印第二个字段(即文件名),后跟 的值x。% cat file1
5f31caf675f2542a971582442a6625f6 /root/md5filescreator/hash1.txt
4efe4ba4ba9fd45a29a57893906dcd30 /root/md5filescreator/hash2.txt
1364cdba38ec62d7b711319ff60dea01 /root/md5filescreator/hash3.txt
% cat file2
163559001ec29c4bbbbe96344373760a /root/md5filescreators/hash1.txt
4efe4ba4ba9fd45a29a57893906dcd30 /root/md5filescreators/hash2.txt
1364cdba38ec62d7b711319ff60dea01 /root/md5filescreators/hash3.txt
% paste file1 file2 | awk '{x = $1 == $3 ? "OK" : "FALSE"; print $2" "x}'
/root/md5filescreator/hash1.txt FALSE
/root/md5filescreator/hash2.txt OK
/root/md5filescreator/hash3.txt OK
Run Code Online (Sandbox Code Playgroud)