Skip to content

Instantly share code, notes, and snippets.

@jaredlt
Last active April 24, 2023 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredlt/eef3ce973b6040f06396976314e9be60 to your computer and use it in GitHub Desktop.
Save jaredlt/eef3ce973b6040f06396976314e9be60 to your computer and use it in GitHub Desktop.
Bulk convert cbr files to cbz
#!/bin/bash
#
# Bulk convert cbr files to cbz
# Will recursively convert all cbr files for the provided path
#
# - Maintains exact file structure within archive
# - Defaults compression level to 'store' (no compression) as most compression already done in the images
#
# Requirements: `sudo apt install unar zip`
#
# Ensure this file is executable: `chmod +x cbr2cbz.sh`
#
# Usage:
# - Place this file 'cbr2cbz.sh' in your file system
# - From the command line, navigate to the directory where you placed it
# - Run using `./cbr2cbz.sh /full/path/to/files/`
# - IMPORTANT: Must use full path, if unsure, navigate to the directory
# you want to convert and type `pwd` (that's the full path)
# - If you run `./cbr2cbz.sh` without a path parameter it will use the
# the current working directory
#
# By default it will leave all original cbr files in place
# Uncomment the 'trash' line below to delete them after successful conversion
# NB. Tested on my own collection but there could still be some edge cases.
# Suggest testing on a small sample first.
#
# Troubleshooting
# - Receiving error: `touch: setting times of '/path/to/file.cbz': Operation not permitted`?
# Run `sudo ./cbr2cbz.sh /full/path/to/files/` as your user may not have permissions to update the file
set -e # exit on error
# If no directory is specified, then use the current working directory (".").
if test -z "$1"; then
SOURCEDIR=`pwd`
else
SOURCEDIR="$1"
fi
echo "Using $SOURCEDIR"
echo "Converting CBRs to CBZs"
# Use RAM disk for temporary files.
WORKDIR="/dev/shm/"
# Separate files using ␜ http://graphemica.com/%E2%90%9C.
# Otherwise it will break on whitespace
# Could use IFS=$'\n' but it's a little more fragile
IFS=""
for INFILE in `find "$SOURCEDIR" -iname "*.cbr" -printf "%p␜"`; do
# Absolute path to old file
OLDFILE=`realpath "${INFILE}"`
# Get the file name without the extension
BASENAME=`basename "${OLDFILE%.*}"`
# Path for the file. The ".zip" file will be written there.
DIRNAME=`dirname "$OLDFILE"`
# Name of the .zip file
NEWNAME="${DIRNAME}/$BASENAME.cbz"
if [ ! -e "${NEWNAME}" ]; then
# Set name for the temp dir. This directory will be created under WORKDIR
TEMPDIR=`mktemp -p "$WORKDIR" -d`
# Create a temporary folder for unRARed files
echo "Extracting $OLDFILE"
# extract rar to temp directory
unar -D -o "${TEMPDIR}/" "$OLDFILE"
# move into temp directory to ensure zip archive matches exactly the original
cd $TEMPDIR
# Zip the files with no compression (store)
zip -Z store -r "$NEWNAME" .
# Preserve file modification time
touch -r "$OLDFILE" "$NEWNAME"
# Delete the temporary directory
rm -r "$TEMPDIR"
# OPTIONAL. Safe-remove the old file
# gio trash "$OLDFILE"
else
echo "${NEWNAME}: File exists!"
fi
done
echo "Conversion Done"
@flying-sausages
Copy link

flying-sausages commented Jan 10, 2021

@wisewtf
Copy link

wisewtf commented Apr 7, 2021

https://gist.github.com/jaredlt/eef3ce973b6040f06396976314e9be60#file-cbr2cbz-sh-L89

You cannot move files to trash from bind mounts so the script failed for me. As there's really no solution to this that I've found, I just changed the trash command for a regular rm

@mslmn
Copy link

mslmn commented Apr 22, 2023

I get this when I run it in terminal on mac
find: -printf: unknown primary or operator
any fixes?

@flying-sausages
Copy link

flying-sausages commented Apr 22, 2023

I get this when I run it in terminal on mac find: -printf: unknown primary or operator any fixes?

@mslmn

quick search gives this https://stackoverflow.com/questions/752818/find-lacks-the-option-printf-now-what

FYI macos uses BSD versions of standard UNIX programs, not the GNU ones that most if not all Linux distros would

@mslmn
Copy link

mslmn commented Apr 23, 2023

Thanks! This fixed the error but now I'm getting a different one

mktemp: illegal option -- p
usage: mktemp [-d] [-q] [-t prefix] [-u] template ...
       mktemp [-d] [-q] [-u] -t prefix 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment