Skip to content

Instantly share code, notes, and snippets.

@futtetennista
Last active November 19, 2024 16:37
Show Gist options
  • Save futtetennista/c68452d62323b99d2da326f00e588ab2 to your computer and use it in GitHub Desktop.
Save futtetennista/c68452d62323b99d2da326f00e588ab2 to your computer and use it in GitHub Desktop.
Script to make it easier to import books in different formats into Calibre using the "Add books > Add from folders and sub-folders" option.
#!/usr/bin/env bash
set -euo pipefail
# Handy script to make it easier to import books in different formats into Calibre using Add books > Add from folders and sub-folders.
function main {
local -r folder="${1?Missing directory}"
if [ ! -d "$folder" ]; then
echo -e "\033[31m'$folder' is not a directory or does not exist\033[0m"
exit 1
fi
cd "$folder"
# List all unique names in the folder excluding hidden files and this script
for filename in $(find . -maxdepth 1 -type f ! -name '.*' ! -name "$(basename "$0")" -exec basename {} \; | sed 's/\(.*\)\..*/\1/' | sort | uniq); do
# Create a subfolder with the name of the file (no suffix)
mkdir -p "$filename"
# Move all files with that name (no suffix) inside the folder
mv "$filename".* "$filename"/
echo "'$filename' processed."
done
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment