如何自动比较大量文件的md5sum哈希值

我可以从终端检查文件的md5sum哈希值,

$ md5sum my_sensitive_file 8dad53cfc973c59864b8318263737462 my_sensitive_file 

但困难的部分是将哈希值与精确值进行比较。

对于大量文件,很难将任何人的32个字符输出与原始/精确哈希值进行比较。 首先,这项工作将非常单调,并且存在很大的错误。

是否可以自动执行比较过程,最好是在CLI中?

例如,我有一个名为test_binary的文件。

MD5文件测试总和是ef7ab26f9a3b2cbd35aa3e7e69aad86c

要自动测试它运行:

 $ md5sum -c <<<"ef7ab26f9a3b2cbd35aa3e7e69aad86c *path/to/file/test_binary" test_binary: OK 

要么

 $ echo "595f44fec1e92a71d3e9e77456ba80d1 filetohashA.txt" | md5sum -c - 

从男人引用

  -c, --check read MD5 sums from the FILEs and check them 

从维基引用

注意:每个md5sum值和要比较的文件名之间必须有两个空格。 否则,将导致以下错误:“找不到格式正确的MD5校验和行”。

链接到维基

你也可以从文件中读取md5哈希值

 $ md5sum -c md5sum_formatted_file.txt 

期待文件的格式:

  

关于MD5和哈希之后的* 。 人类很少注意:

  When checking, the input should be a former output of this program. The default mode is to print a line with checksum, a character indicating input mode ('*' for binary, space for text), and name for each FILE. 

这里是stackoverflow的链接,在那里我找到了问题的答案,为什么我们有时应该区分binary文件和text文件。


一种可能性是使用实用程序cfv

 sudo apt-get install cfv 

CFV支持许多类型的哈希,以及测试和哈希文件创建。

 # List the files $ ls test.c # Create a hash file $ cfv -tmd5 -C temp.md5: 1 files, 1 OK. 0.001 seconds, 302.7K/s # Test the hash file $ cfv -tmd5 -T temp.md5: 1 files, 1 OK. 0.001 seconds, 345.1K/s # Display the hash file $ cat *.md5 636564b0b10b153219d6e0dfa917d1e3 *test.c 

是的,此命令需要星号* 。 看看这个例子。

这是二进制文件,让我们说正确的md5sum值是exampleofcorrectmd5value00000000 (32hex字符)

 [root@Linux update]# ls -lh total 137M -rw-r--r-- 1 root root 137M Nov 5 13:01 binary-file.run.tgz [root@Linux update]# 

-c, – check

从文件中读取MD5总和并检查它们

如果md5sum值与二进制文件匹配,您将获得此输出

 [root@Linux ~]# md5sum -c <<< "exampleofcorrectmd5value00000000" *binary-file.run.tgz" binary-file.run.tgz: OK [root@Linux ~]# 

这是当md5sum值不匹配时

 [root@Linux update]# md5sum -c <<< "exampleofwrongmd5value0000000000 *binary-file.run.tgz" binary-file.run.tgz: FAILED md5sum: WARNING: 1 of 1 computed checksum did NOT match [root@Linux update]# 

如果没有星号* ,即使md5值正确,您也会收到以下错误消息

 [root@Linux ~]# md5sum -c <<< "exampleofcorrectmd5value00000000 binary-file.run.tgz" md5sum: standard input: no properly formatted MD5 checksum lines found [root@Linux ~]# 

此外,如果md5sum中没有32个hex字符,您将收到相同的错误消息。 在此示例中,它只有31个字符。

 [root@Linux ~]# md5sum -c <<< "exampleofmd5valuelessthan32char *binary-file.run.tgz" md5sum: standard input: no properly formatted MD5 checksum lines found [root@Linux ~]# 

许多文件的解决方案

如果您有许多文件并希望自动执行该过程,则可以按照以下步骤操作:

 user@Ubuntu:~$ ls -lh total 12K -rw-rw-r-- 1 user user 4 Nov 5 14:54 file-a -rw-rw-r-- 1 user user 4 Nov 5 14:54 file-b -rw-rw-r-- 1 user user 4 Nov 5 14:54 file-c user@Ubuntu:~$ 

为每个文件生成md5sum并将其保存到md5sum.txt

 user@Ubuntu:~$ md5sum * | tee md5sum.txt 0bee89b07a24ae27c83fc3d5951213c1 file-a 1b2297c171a9a450d184871ccf6c9ad4 file-b 7f4d13d9b0b6ac086fd68637067435c5 file-c user@Ubuntu:~$ 

要检查md5sum以查找所有文件,请使用以下命令。

 user@Ubuntu:~$ md5sum -c md5sum.txt file-a: OK file-b: OK file-c: OK user@Ubuntu:~$ 

如果md5sum值与文件不匹配,则为此示例。 在这种情况下,我将修改file-b内容

 user@Ubuntu:~$ echo "new data" > file-b user@Ubuntu:~$ 

看,这是错误消息。 希望这可以帮助。

 user@Ubuntu:~$ md5sum -c md5sum.txt file-a: OK file-b: FAILED file-c: OK md5sum: WARNING: 1 computed checksum did NOT match user@Ubuntu:~$