this post was submitted on 17 Apr 2025
8 points (100.0% liked)

Linux

54020 readers
1242 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 6 years ago
MODERATORS
 

Hi all. This is an update on my script extracting CRC32 checksum from the 7z commandline tool. The output should be similar to how the md5sum tool outputs, the checksum and the file name/path.

The initial version of this script was actually broken. It would not output all files if a directory was included (wrong counting of files through argument number). Also filenames that contained a space would only output the first part until the space character. All of this rookie mistakes are solved. Plus there is a progress bar showing what files are processed at the moment, instead showing a blank screen until command is finished. This is useful if there are a lot of files or some big files to process.

Yes, I'm aware there are other ways to accomplish this task. I would be happy to see your solution too. And if you encounter a problem, please report.

crc32sum:

(Note: Beehaw does not like the "less than" character and breaks the post completley. So replace the line cat %%EOF with or copy it from the Github Gist link below:)

#!/usr/bin/env bash

if [[ "${#}" -eq 0 ]] || [[ "${1}" == '-h' ]]; then
    self="${0##*/}"
    cat %%EOF
usage: ${self} files...

Calculate CRC32 for each file.

positional arguments:
  file or dir       one or multiple file names or paths, if this is a directory
                    then traverse it recursively to find all files
EOF
    exit 0
fi

7z h -bsp2 -- "${@}" |
    \grep -v -E '^[ \t]+.*/' |
    \sed -n -e '/^-------- -------------  ------------$/,$p' |
    \sed '1d' |
    \grep --before-context "9999999" '^-------- -------------  ------------$' |
    \head -n -1 |
    \awk '$2=""; {print $0}'

top 4 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

I was actually looking for something like this a few days ago. This is pretty useful as there's no crc32sum readily available on linux. Thanks for that!

I would personally change a few things, mostly small nitpicks to be fair.

  1. Prefer [[ ]] over [ ] for tests. Source: https://www.shellcheck.net/wiki/SC2292
  2. Use $0 instead of hardcoding crc32sum in the help messages. That way it will work even if someone names the script differently
  3. You could exit 0 after the help and end the if there instead of having the whole work being done in an else.

As I said, nitpicks!

Also, I like that your example uses *.smc, that's also the reason I needed a crc32sum 🤣

[–] [email protected] 2 points 3 weeks ago* (last edited 3 weeks ago)

Agreed on your points and usually I do 2. (name) and 3. (exit instead else) sometimes. For the [[ over [, it usually matters only for word splitting and globbing behavior, if you do not enclose the variables between quotes I believe. But looking into the shellcheck entry, looks like there is no disadvantage. I may start doing this by default in the future too.

So thanks for the suggestions, I will update the script in a minute.

Edit: I always forget that Beehaw will break if I use the "lower than" character like in , so I replaced it in the post with cat %%EOF which requires to change that line. And the example usage is gone for the moment.

Edit2 (21 hours later): I totally forgot to remove the indentation and else-branch. While doing so I also added a special option -h, in case someone tries that. Not a big deal, but thought this should be.

[–] [email protected] 2 points 3 weeks ago (1 children)

why use crc32 though? do you have some program that only works with that?

[–] [email protected] 2 points 3 weeks ago

crc32 is very common amongst emulation and roms. While often they provide md5sum too, crc32 is a bit faster on bulk.