Skip to content

Instantly share code, notes, and snippets.

@iemcd
Last active May 8, 2020 19:57
Show Gist options
  • Save iemcd/484f2fe3d9bcef5e782fdb88d04b35dd to your computer and use it in GitHub Desktop.
Save iemcd/484f2fe3d9bcef5e782fdb88d04b35dd to your computer and use it in GitHub Desktop.
A script to clean-up cbz and cbr files
#!/bin/bash
#TODO: evaluate using --store option for recompression
#TODO: fix case-sensitive match in basename (not an issue for me)
shopt -s nocaseglob
suffix='(clean)'
maxjobs=16
function shrink
{
chmod -R 755 ./*
rm -rf "__MACOSX"
rm -rf ".DS_Store"
# flatten directories
find . -mindepth 2 -type f -exec mv -t . -i '{}' +
# optimize any pngs
optipng -q -clobber -fix ./*.png
# optimize any jpegs
jpegoptim -q --all-progressive ./*.jpg
jpegoptim -q --all-progressive ./*.jpeg
}
function open_cbz
{
echo "Cleaning $1..."
local bkname=$(basename "$1" '.cbz')
local parent=$(pwd)
local bkpath="/dev/shm/$bkname"
unzip -q "$1" -d "$bkpath"
cd "$bkpath"
shrink
zip -qrj "$parent/$bkname$suffix.cbz" .
cd "$parent"
rm -rf "$bkpath"
}
function open_cbr
{
echo "Cleaning $1..."
local bkname=$(basename "$1" '.cbr')
local parent=$(pwd)
local bkpath="/dev/shm/$bkname(rar)"
mkdir "$bkpath"
unrar x -idq "$1" "$bkpath"
cd "$bkpath"
shrink
zip -qrj "$parent/$bkname$suffix(rar).cbz" .
cd "$parent"
rm -rf "$bkpath"
}
for book in *.cbz *.cbr
do
while [[ $(jobs | wc -l) -ge $maxjobs ]]
do
sleep 2
done
if [[ "$book" = *.cbz ]]
then
open_cbz "$book" &
elif [[ "$book" = *.cbr ]]
then
open_cbr "$book" &
fi
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment