miriam_e: from my drawing MoonGirl (Default)
[personal profile] miriam_e
I download a lot of ebooks from Project Gutenberg, the Internet Archive, Gutenberg Australia, Cory Doctorow's craphound site, and various other places. Usually the naming of the file is not sensible. For example, the epub ebook of Jules Verne's "Journey to the Centre of the Earth" on Project Gutenberg (the version with illustrations) is titled "pg18857-images.epub". Many other sites use the old paper-book library standard of reversing an author's name as 'surname, first name' which doesn't make sense on computers. Normally I have to rename these each time, but if I'm downloading works by an author like Jules Verne who has many books it can become a little tedious.

I've written a tiny script that saves me the trouble. It uses the nice commandline program ebook-meta that comes as a part of the lovely program Calibre.

My script uses ebook-meta to extract the information to a temporary file, then uses sed to pull out the author's name (reversing it if need be) and pull out the title. Then it renames the original file as 'author - title'

I've put the little script behind the cut-tag:


#!/bin/sh

# ebook-rename
# by Miriam  2013-06-27,28,29
#
# uses calibre's ebook-meta to read out the data
# then sed to extract the author and title
# and renames the original file


# test to see if started from CLI or icon
#tstcli=`tty | head -c3`
#if [ "$tstcli" = "not" ]; then
#   #xmessage "`tty`"
#   xmessage "EEEK!! Don't click here!"
#   exit
#fi

# change dir to path of file
cd "${1%/*}"

function show_options {
   echo -e "\e[34musage: \e[35mebook-rename \e[34m"
   echo -e "  Renames an ebook file to 'author - title'"
   echo -e "  by extracting info from the ebook's metadata.\e[30m"
}

if [ "$1" = "" -o "$1" = "-h" ]; then
   show_options
else

   # I had previously over-complicated the reading of meta-data
   # by using the switch --from-opf not realising that I don't need any switches
   # in order to read meta-data. I know in retrospect this seems obvious, but
   # in my defense this doesn't seem to be mentioned in the docs at all.
   ebook-meta "$1" >/tmp/ebook-meta-output

   # get author
   # ----------
   # send the metadata via cat
   # sed extracts the author line
   # sed extracts the author's name from that line
   # sed re-arranges the name if it has surname first
   # sed turns dots into spaces (they interfere with easy use of rox for various operations)
   # sed turns multiple spaces into one
   # sed gets rid of any leading spaces, just in case
   author=`cat /tmp/ebook-meta-output | sed -n '/^Author/p' | sed 's/.*: \(.*\)/\1/' | sed 's/^\(.*\).\[.*/\1/' | sed 's/\(.*\), \(.*\)/\2 \1/' | sed 's/\./ /g' | sed 's/  */ /g' | sed 's/^ *//'`

   # get title
   # ---------
   # send the metadata via cat
   # sed extracts the title line
   # sed extracts the title from that line
   title=`cat /tmp/ebook-meta-output | sed -n '/^Title   /p' | sed 's/[^:]*: \(.*\)/\1/' | sed 's|/|-|g'`

   # get filename without path
   fname="${1##*/}"
   # get filename without extension
   bname="${fname%.*}"
   # get extension
   ename="${fname##*.}"

   # rename it
   images=`sed 's/.*-\(images\)/ (+\1)/' <<<"$bname"`
   if [ "$images" = "$bname" ] ; then images="" ; fi
   if [ -e "${author} - ${title}${images}.${ename}" ] ; then
      echo "Can't rename -- \"${author} - ${title}${images}.${ename}\" already exists"
   else
      mv "$1" "${author} - ${title}${images}.${ename}"
   fi

fi

Profile

miriam_e: from my drawing MoonGirl (Default)
miriam_e

December 2025

S M T W T F S
 123456
7 8 910 111213
1415 1617181920
21222324252627
28293031   

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 29th, 2025 11:37 am
Powered by Dreamwidth Studios