Linux 大文件crc计算,cksum(计算每个输入文件的循环冗余校验(CRC)和字节数,并将其写入标准输出)_语法_示例_Unix&Linux命令_uc电脑园官网...

The checksum of a file is a simple way to check if its data has become corrupted when being transferred from one place to another. If the checksum value of the file is the same before and after being transferred, it is unlikely that any data corruption has accidentally occurred — from signal noise, for example.

Let's say you have a file, myfile.txt, containing the following text:

This is my original file.

You can calculate the checksum using cksum:

cksum myfile.txt

...and this will be the output:

4164605383 26 myfile.txt

Here, 4164605383 is the checksum, and 26 is the amount of data, in bytes. If you change the contents of the file to this:

This is no longer my original file.

...and run cksum again, you will see the following:

cksum myfile.txt

632554699 36 myfile.txt

The checksum is very different, and we can also see that there are ten more bytes of data.

The checksum will be different even if the number of bytes is same as the original:

This is a corrupted file.

cksum myfile.txt

2256884274 26 myfile.txt

...and it will change dramatically even if only one character is different:

This is my original file?

cksum myfile.txt

3832066352 26 myfile.txt

Important: Simple checksums, such as those produced by the cksum tool, are useful only for detecting accidental data corruption. It's not meant to protect against malicious alteration of a file. It's been proven that an attacker could carefully make changes to a file that would produce an identical cksum checksum. Therefore, if you need to be absolutely certain that a file is identical to the original, use a more powerful method. We highly recommend using the SHA256 algorithm for verifying data integrity. You can generate and verify SHA256 hash sums using tools such as GNU rhash.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下 Perl 代码来完成你的需求: ```perl use strict; use warnings; # 获取当前文件夹下所有以 ".png" 结尾的文件 my @files = glob "*.png"; # 打开 CRC 文件用于写入校验结果 open my $crc_fh, '>', 'CRC.txt' or die "无法打开 CRC 文件: $!"; # 遍历每个文件计算校验和并写入 CRC 文件 foreach my $file (@files) { # 使用 cksum 命令计算校验和 my $cksum_output = `cksum $file`; # 提取校验和结果 my ($checksum, $size, $filename) = split /\s+/, $cksum_output; # 将校验结果写入 CRC 文件 print $crc_fh "$checksum\n"; } # 关闭 CRC 文件 close $crc_fh; # 读取 CRC 文件中的每一行并存储到队列中 my @queue; open $crc_fh, '<', 'CRC.txt' or die "无法打开 CRC 文件: $!"; while (my $line = <$crc_fh>) { chomp $line; push @queue, $line; } close $crc_fh; # 比较队列中的值 if (scalar @queue > 1) { my $first_value = shift @queue; my $same_values = 1; foreach my $value (@queue) { if ($value ne $first_value) { $same_values = 0; last; } } if ($same_values) { print "队列中的所有值相同\n"; } else { print "队列中的值不全相同\n"; } } else { print "队列中的值不足两个\n"; } ``` 这段代码首先获取当前文件夹下所有以 ".png" 结尾的文件,然后使用 `cksum` 命令计算每个文件的校验和,并将校验结果依次写入到名为 "CRC.txt" 的文件中。接着,读取 "CRC.txt" 文件中的每一行并存储到队列中。最后,比较队列中的值,如果所有值相同,则输出 "队列中的所有值相同";否则,输出 "队列中的值不全相同"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值