How to extract first file from cbz files in one batch?
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"