Last active
April 24, 2023 14:07
-
-
Save jaredlt/eef3ce973b6040f06396976314e9be60 to your computer and use it in GitHub Desktop.
Bulk convert cbr files to cbz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
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
I get this when I run it in terminal on mac
find: -printf: unknown primary or operator
any fixes?
I get this when I run it in terminal on mac
find: -printf: unknown primary or operator
any fixes?
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
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
https://gist.github.com/jaredlt/eef3ce973b6040f06396976314e9be60#file-cbr2cbz-sh-L74
edit I did not know that's a thingunar
->unrar