Skip to content

Instantly share code, notes, and snippets.

@kinwahlai
Created April 13, 2025 10:57
Show Gist options
  • Save kinwahlai/1e8e0b7c4c634539de9644d8e71e088b to your computer and use it in GitHub Desktop.
Save kinwahlai/1e8e0b7c4c634539de9644d8e71e088b to your computer and use it in GitHub Desktop.
Script to build EPUB file from Apple Books folder format

Script to build EPUB file from Apple Books folder format

credit: eroux https://www.mobileread.com/forums/showpost.php?p=4304758&postcount=146

Create a bash script call ePubFolder2File.sh with the following content:

#!/usr/bin/env bash

main_loop()
{
    opts=':';
    while getopts ${opts} opt; do
        case "${opt}" in
            [?]|*)
                echo "usage: $(basename "$0") EPUB_FOLDER" >&2
                exit 1;
            ;;
        esac
    done
    shift $((OPTIND - 1));

    while [[ "$1" != "" ]]; do
        build_epub "${1}"
        shift;
    done
}

build_epub ()
{
    local source_folder="$1"
    local dest_file
    local current_dir=$(pwd)

    dest_file="$(basename "${source_folder%.epub}").epub"

    if [[ $(dirname "${source_folder}") == "." ]] && [[ "${source_folder}" != "${source_folder%.epub}" ]]; then
        source_folder="${source_folder%.epub}"
        mv "${source_folder}.epub" "${source_folder}"
    fi

    cd "${source_folder}" || exit 1
    zip --quiet -X0 "${current_dir}/${dest_file}" mimetype
    zip --quiet -rDX9 "${current_dir}/${dest_file}" * -x "*.DS_Store" -x mimetype
}

main_loop "$@"

Make the script executable

chmod +x ePubFolder2File.sh

Run the script on all EPUB folders in the Apple Books directory

(Change the path to your Apple Books directory if necessary)

find "/Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks/Documents" -type d -name \*.epub | 
    while read epubdir; do 
        if [[ ! -f  $(basename "$epubdir") ]]; then 
            echo "Processing $(basename $epubdir)"; 
            ePubFolder2File "$epubdir"; 
        fi;
    done

Convert 1 Apple Books ePUB folder to a epub file

ePubFolder2File /Users/USERNAME/Library/Mobile\ Documents/iCloud~com~apple~iBooks/Documents/BookName.epub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment