Skip to content

Bulk eBook Converter

   

Script that wraps the ebook-converter linux command line tool for a convenient bulk usage.

This is a simple bash script that uses ebook-converter to handle bulk eBook format conversion.
So it ebook-converter needs to be installed for this to work.

#!/bin/sh
DIR="$1"
INTYPE="$2"
OUTTYPE="$3"

# default to current dir and input=epub, output=mobi
[ "$DIR" == "" ] && DIR="."
[ "$INTYPE" == "" ] && INTYPE="epub"
[ "$OUTTYPE" == "" ] && OUTTYPE="mobi"

# save and change IFS
OLDIFS=$IFS
IFS=$'\n'
 
# read all file name into an array
fileArray=($(find "$PWD"/$DIR -type f |grep "\.$INTYPE$"))
 
# restore it
IFS=$OLDIFS
 
# get length of an array
tLen=${#fileArray[@]}
 
# loop all files
for (( i=0; i<${tLen}; i++ ));
do
  echo "Converting ${fileArray[$i]}"
  ebook-convert "${fileArray[$i]}" "$(basename "${fileArray[$i]}" .$INTYPE).$OUTTYPE"
done

#Credits: base script inspired from https://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html

The code can be found in https://github.com/miguelabate/bash-scripts/blob/master/bulk-ebook-converter.sh

Usage

./bulk-ebook-converter.sh <some-dir> <input-format> <output-format>

For example:

./bulk-ebook-converter.sh mybooks epub mobi

This command will find all the .epub files under mybooks directory in a recursive way and generate the .mobi files in the current folder (where the command is running).

If not specified, the default input format is “epub”, the output is “mobi” and the the directory used is the current one.

Related Posts

  1. Reddit Urls Scraper