Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fabiolimace/29becacc708a5f2bb4df0f11fb7743e9 to your computer and use it in GitHub Desktop.

Select an option

Save fabiolimace/29becacc708a5f2bb4df0f11fb7743e9 to your computer and use it in GitHub Desktop.
Calibre Library Scripts
#!/bin/bash
#
# List all books in a Calibre library whose MOBI-ASIN is not listed in a file.
#
# Read all "metadata.opf" files in a Calibre library, find each MOBI-ASIN identifier,
# and check if it exists in a list. If the MOBI-ASIN does not exist in the list,
# print the folder where the "metadata.opf" is.
#
#
# It can be used to know which ebooks of a Calibre library are not in second Calibre library.
# But you first have to generate a list of MOBI-ASIN identifiers from the second library.
# Use the script `calibre-list-mobi-asin.sh` to generate the input list in the second library.
#
# Usage
#
# calibre-find-ebooks-missing-in-a-list.sh MOBI_ASIN_LIST.txt
#
MOBI_ASIN_LIST=${1}
[ -z "${MOBI_ASIN_LIST}" ] && exit 1;
LIBRARY=${1}
[ -d "$LIBRARY" ] || LIBRARY=$(pwd);
time find "${LIBRARY}" -type f -name "metadata.opf" | while read METADATA; do
MOBI_ASIN=`grep -E '<dc:identifier opf:scheme="MOBI-ASIN">B[A-Z0-9]{9}</dc:identifier>' "${METADATA}" | grep -E --only-matching 'B[A-Z0-9]{9}'`;
if [ -n "${MOBI_ASIN}" ]; then
COUNT=`grep -Ec "^${MOBI_ASIN}$" "${MOBI_ASIN_LIST}"`
if [ ${COUNT} -eq 0 ]; then
FOLDER=`dirname "${METADATA}"`
echo ${FOLDER}
fi;
fi;
done;
#!/bin/bash
#
# Generates a list of checksums for all ebooks in a library.
#
# It lists the MD5 sums in the output file.
#
# Usage
#
# calibre-library.checksum.sh LIBRARY
#
LIBRARY=${1}
[ -d "$LIBRARY" ] || LIBRARY=$(pwd);
time find "$LIBRARY" -type f -iregex ".*\.\(azw.?\|kfx\|mobi\|epub\|htmlz\|pdf\)" -exec md5sum "{}" \; \
| tee "$LIBRARY"/calibre-library.checksum.v`date +"%Y%m%d"`.txt
#!/bin/bash
#
# Generates a list of sizes in bytes for all ebooks in a library.
#
# It lists the results of `wc -c` in the output file.
#
# Usage
#
# calibre-library.wc.sh LIBRARY
#
LIBRARY=${1}
[ -d "$LIBRARY" ] || LIBRARY=$(pwd);
time find "$LIBRARY" -type f -iregex ".*\.\(azw.?\|kfx\|mobi\|epub\|htmlz\|pdf\)" -exec wc -c "{}" \; \
| tee "$LIBRARY"/calibre-library-wc.v`date +"%Y%m%d"`.txt
#!/bin/bash
#
# List all MOBI-ASIN codes extracted from OPF files.
#
# Usage
#
# calibre-list-mobi-asin.sh LIBRARY
#
LIBRARY=${1}
[ -d "$LIBRARY" ] || LIBRARY=$(pwd);
time find "$LIBRARY" -type f -name "*.opf" -exec grep -E '<dc:identifier opf:scheme="MOBI-ASIN">B[A-Z0-9]{9}</dc:identifier>' "{}" \; \
| grep -E --only-matching 'B[A-Z0-9]{9}' | sort | tee "$LIBRARY"/calibre-list-mobi-asin.v`date +"%Y%m%d"`.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment