Below is a script that will convert e-books of various formats to PDF for the sake of compatibility. (I have tested this, and it works very well.)
Features:
Supported input formats: EPUB, MOBI, AZW, AZW3, IBA and OPF.
Simplicity. Just run the script and type the input directory; the script will do the rest.
It won't mess anything up: The script will not alter or delete any of your current files or directories. In case you want to convert a collection of EPUB-files to PDF, the PDF files will merely be added to the same folder as the EPUB-files.
Easy to deal with large collections: Since everything works automatically, it is ideal for large collections with many files and folders.
Prerequisites:
You need to install Calibre to make this work.
Let's install Calibre (as root):
Code: Select all
# apt install calibre
Create a file called .ebookconverter.sh
Code: Select all
$ nano ~/.ebookconverter.sh
Copy and paste the following content into the file
Code: Select all
#!/bin/bash
# Recursive e-book to PDF converter. Supported input formats: EPUB, MOBI, AZW, AZW3, IBA, and OPF
# Check if Calibre is installed
command -v ebook-convert >/dev/null 2>&1 || {
echo >&2 "Calibre is not installed. Please install Calibre (e.g., # apt install calibre) and try again."
exit 1
}
# Prompt for input directory
read -p "Enter input directory: " input_directory
# Check if input directory exists
if [ ! -d "$input_directory" ]; then
echo "Input directory not found: $input_directory"
exit 1
fi
# Check if output file exists
check_output_file_exists() {
output_file=$1
if [ -f "$output_file" ]; then
echo "Output file already exists: $output_file"
return 0
else
return 1
fi
}
# Check file formats and convert
convert_to_pdf() {
ebook_path=$1
# Check if e-book
extension="${ebook_path##*.}"
if [[ "$extension" =~ ^(epub|mobi|azw|azw3|iba|opf)$ ]]; then
output_file="${ebook_path%.*}.pdf"
# Check if output file exists
if check_output_file_exists "$output_file"; then
echo "Skipping conversion for existing file: $ebook_path"
return
fi
echo "Converting: $ebook_path"
# Find and convert e-book to PDF
ebook-convert "$ebook_path" "$output_file" >/dev/null
if [ $? -eq 0 ]; then
echo "Successfully converted: $ebook_path"
else
echo "Failed to convert: $ebook_path"
fi
else
echo "Skipping PDF or non-ebook file: $ebook_path"
fi
}
recursive_convert() {
path=$1
for file in "$path"/*; do
if [ -d "$file" ]; then
recursive_convert "$file"
elif [ -f "$file" ]; then
convert_to_pdf "$file"
fi
done
}
recursive_convert "$input_directory"
Make the script executable
Code: Select all
$ chmod +x ~/.ebookconverter.sh
Run the script
Just open the terminal and use the following command to run the script:
Code: Select all
./.ebookconverter.sh