Last active
November 19, 2024 16:37
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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