Skip to content

Instantly share code, notes, and snippets.

@prehensilecode
Last active June 21, 2025 16:11
Show Gist options
  • Save prehensilecode/5d32be1a00cd756add9e1129a6b43283 to your computer and use it in GitHub Desktop.
Save prehensilecode/5d32be1a00cd756add9e1129a6b43283 to your computer and use it in GitHub Desktop.
Convert single Apple epub to standard epub
#!/bin/bash
# Based on: https://www.reddit.com/r/Calibre/comments/dza1zi/comment/lxz97q5/
# Except this just does a single Apple epub, which is a directory (folder);
# loop over it to do a directory.
# WARNING: DOES NOT HANDLE BOOKS WITH DRM
# Downloaded Apple Books books are in:
# ~/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books
# Books in iCloud are in:
# ~/Library/Mobile Documents/iCloud~com~apple~iBooks/Documents
cwd=$( pwd )
title=""
if [[ -d "$1" ]]
then
cd "$1"
export title="$( grep -A1 itemName iTunesMetadata.plist | grep \<string\> | cut -f2 -d'>' | cut -f1 -d'<' )"
echo "Found: ${title}"
find . -name "iTunesMetadata*.plist" -delete 2> /dev/null
zip -0X "~/Downloads/tmp.epub" mimetype > /dev/null
zip -rDX9 "~/Downloads/tmp.epub" * -x "*.DS_Store" -x mimetype > /dev/null
echo "Converting: $1 -> \"${title}.epub\""
cd $cwd
mv ~/Downloads/tmp.epub "${title}.epub"
else
echo "ERROR: ${1} is not an Apple Books epub. Ignoring."
exit 1
fi
#!/bin/bash
# To convert all Apple books; copies all converted books to the Downloads folder
# Only downloaded books
# /bin/cp -R ~/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books/*.epub $TMPDIR
# All iCloud books
/bin/cp -R ~/Library/Mobile\ Documents/iCloud~com~apple~iBooks/Documents/*.epub $TMPDIR
cd $TMPDIR
for book in *.epub
do
apple_epub_to_std_epub.sh "${book}"
/bin/rm -rf "${book}"
done
cp *.epub ~/Downloads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment