write_pdf_metadata_in_calibre.sh 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/bash
  2. #
  3. # Scans the given directory and all subdirectories for the file
  4. # "metadata.opf". This file is created by Calibre. From this file data will be
  5. # written into the PDFs, which are exactly in the same directory.
  6. #
  7. #
  8. # REQUIREMENTS: calibre by KOVID GOYAL (http://calibre-ebook.com/)
  9. #
  10. shopt -s nullglob
  11. shopt -s nocaseglob
  12. write_metadata() {
  13. find "$1" -depth -type f -name "metadata.opf" | { while read -r metadata_path;
  14. do
  15. echo "metadata found: $metadata_path"
  16. dir_name=$(dirname "$metadata_path")
  17. pdfs=$(find "$dir_name" -maxdepth 1 -type f -name '*.pdf' | wc -l)
  18. echo "pdfs found: $pdfs"
  19. find "$dir_name" -maxdepth 1 -type f -name "*.pdf" | { while read -r pdf_path;
  20. do
  21. ebook-meta --from-opf="$metadata_path" "$pdf_path" 2>/dev/null
  22. done
  23. echo
  24. }
  25. done
  26. }
  27. }
  28. [[ -z "$1" ]] && logError "Please specify a start directory." && exit 1
  29. if [ -f "$1" ]
  30. then
  31. search_path=$(dirname "$1")
  32. else
  33. search_path="$1"
  34. fi
  35. write_metadata "$search_path"
  36. exit 0