poor man's cfv/cksfv (CRC checksum)

Lately I wanted to compute CRC-32 checksums for some videos. At first, I took a look at Digest::CRC and wrote this one-liner:

perl -MDigest::CRC=crc32_hex -E 'say crc32_hex <>' file.mkv

But of course that didn't work, since I was only reading a line off the first file in @ARGV. Realizing I need to read in the file, I added IO::All in the mix:

perl -MDigest::CRC=crc32_hex -MIO::All -E 'say crc32_hex io($ARGV[0])->all' file.mkv

That worked, but it was too slow since it read files into a string, and I was dealing with large (~1GB) file sizes. After a bit of looking around, I found the Digest::file module, so my one-liner finally becomes:

perl -MDigest::file=digest_file_hex -E 'say "$_ cksum: \U@{[digest_file_hex $_, q|CRC-32|]}\E" for @ARGV' file1.mkv file2.mkv files*.avi

Of course, I could have gotten something like cfv or cksfv from my Ubuntu repository, but curiosity got the better of me ;)

(edited: make use of perl's -E switch to implicitly enable features.)

1 Comment

The last command is very interesting for calculate CRC of big files with one Perl line.

But if you are runing Linux, why not simply use the cksum tool directly from the console.

Leave a comment

About Zak B. Elep

user-pic Hacker, Gamer, Married. Runs Slackware on laptops, OpenBSD on servers, and Perl on everything. Even on Docker.