3

I have a collection of comic books that I store on my computer, mostly in .cbz format (which is just a ZIP archive, but with a fancy name). Recently, I decided to make a collage of the covers of a specific comic series, for which I'd need the first file in the comic book archive.

Here's the breakdown of the problem:

  • 75 .cbz files in a single folder;
  • Each file has a name like <series name> #<issue number>, where issue numbers go from 1 to 75 (one problem: it's not like 01, 02, 03, etc, so there may be a confusion with sorting there);
    • Each file inside each archive is named like <issue name> <3-digit issue number with preceding zeros>
  • Archives can be renamed to .zip if necessary, but I know 7Zip (and some other archivers, but not unrar) handles CBZ well enough;

What I need:

  • The first file from each of the 75 archives. The files already have distinctive names, so there's no need to rename them at the output.

What I have:

  • Ubuntu 16.04
  • 7Zip
  • Internet connection

How do I make this happen? What command will extract the first file from every archive in a folder to the same folder?

5

If you don't need to rename output files then I suggest something like this:

#!/bin/bash

for file in *.cbz
do
 cover="`zipinfo -2 "$file" | awk 'NR==2 {exit} 1'`"
 unzip -j "$file" "$cover"
done;

Save it in a file like script.sh and put it into the same directory as your comics are. Then give it execute permission:

chmod +x script.sh

And run it:

./script.sh

How does it work?

We can get a list of all files within a zip archive using zipinfo:

zipinfo -2 mycomic.cbz

it outputs something similar to:

First file within the archive.jpg
Second file within the archive.jpg
...

Then using awk 'NR==1 { print }' we can return the first file name which is: First file within the archive.jpg.

Now to extract this file I can use a unzip like:

unzip -j mycomic.cbz "First file within the archive.jpg"
  • Got an error for each file without any output: Archive: Lucifer #10.cbz caution: filename not matched: – Gallifreyan May 28 '17 at 15:11
  • @Gallifreyan Updated, check it again ;) – Ravexina May 28 '17 at 15:14
  • It worked!... But extracted the 2nd page of every issue (i.e. the files end with 002) :D – Gallifreyan May 28 '17 at 15:18
  • Change the NR==2 to NR==1 see if you get the desired result ;) – Ravexina May 28 '17 at 15:19
  • Yep, that worked perfectly! Now how does that magic work, really? Would it work if the files in the archives weren't enumerated? – Gallifreyan May 28 '17 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.