I would like to use a command line to extract the title of a book (possibly also other metadata) from its epub file and return it as a string.

I am using Linux, but my guess is that the question makes sense in any other environment.

I was expecting to easily find a clear and simple answer by serching the web. But that is not apparently the case, or I am still to ignorant to recognize answers.

The command could look like: getbookinfo -m title myfile.epub

and would yield the title of the book found in the epub file.


Here's a quick bash script (with no error-checking whatsoever) to do what you want:

if [ $# -lt 3 ]
   echo "Usage: minfo -m <meta-type> <epub-file>"
   fileloc=`unzip -l "$3" | grep -Po '\b[^\s-]*\.opf\b'`
   metafound=`zipgrep  '<dc:'$2'>(.*)</dc:'$2'>' "$3" $fileloc`
   echo `expr "$metafound" : '.*<dc:'$2'>\(.*\)</dc:'$2'>.*'`

This uses unzip -l to find out where the .opf file is in the .epub (normally OEBPS\content.opf, but it can be named anything as long as it has the .opf extension). Then it uses zipgrep to find occurrences of the desired metadata type in that file. Finally, strip off the tags to leave just the metadata.

And here's a test run:

beaker$ ./minfo -m title Make_Electronics.epub 
Make: Electronics
beaker$ ./minfo -m publisher Make_Electronics.epub 
O'Reilly Media, Inc.
beaker$ ./minfo -m subject Make_Electronics.epub 


That last line is blank because the metadata entry for subject in the opf file is:

| improve this answer | |
  • 1
    Very nice. Pretty-much what I wanted. I was checking your code and wanted to ask you about the part you just changed (xxx.opf), as it looked a bit strange. I did not know zipgrep. I slightly changed your script to have less problems with exit depending on how the script is used. Another point: when several meta fields with differing values, the script returns the last one. Also, some meta fileds seem to have a more complicated structure: <dc:creator opf:role="aut">John Doe</dc:creator>. Then I checked only one epub file. But I learned a bunch of things on dealing with such problems. – babou Mar 28 '16 at 20:46
  • @babou Good edit, thanks. I can modify the regex to handle the case you bring up of the <dc:creator... if you like. Meanwhile I'll have a look at returning multiple values. – beaker Mar 28 '16 at 21:07
  • Would be great. But I reached my available limit for rep brownie points :-). Then do not spend too much time on it, as there will always more that is asked. I am only surprised the is no standard set of functions for simple manipulation of epub. I am only interested in what does not carry DRM, and I am not even sure it matters much for the rest. – babou Mar 28 '16 at 21:24
  • What is the "-Po" argument? OS X grey has no -P :-( – Paul Waldo Jul 13 '17 at 18:14
  • 1
    @PaulWaldo See stackoverflow.com/questions/16658333/… – beaker Jul 13 '17 at 18:21

exiftool can read (but not write) epub meta data. For example

exiftool -T -Title main.epub

| improve this answer | |
  • This is actually an excellent answer, which furthermore works also for mobi or PDF (though the metadata info in PDF is not always very informative). It will list complete metadata if the desired one is not specified: exiftool main.epub. If I had not already voted for an answer which helped me a lot, I would choose you as best answer (I know I can change). – babou Jun 14 '17 at 17:34
  • Made a post below with way more examples – SuperMar1o Sep 12 at 0:37

An EPUB file is just a zip file, and the book's metadata is contained in the OPF (Open Packaging Format) file, which is an XML file. The title is located in the /package/metadata/dc:title element. The other info you're looking for is probably also in children of the metadata element. Here's a good intro to the EPUB format: http://www.ibm.com/developerworks/xml/tutorials/x-epubtut/.

I'm not very familiar with Linux, so I don't know exactly how you'd do all this from the command line. I suspect it would involve writing or finding a script that would uncompress or dig around in the EPUB file, find the OPF file, parse its XML, and locate the element with the metadata you specify, perhaps using XPath.

| improve this answer | |
  • Thanks for the pointers. Actually, I was looking for a solution that does a minimal amount of computation. I was going to try it based on your answer, but the next answer seems to do the job. – babou Mar 28 '16 at 19:33

To print out epub metadata,

exiftool yourbook.epub

To edit the metadata, you'd need to unzip it and then zip it back again, as mentioned by others, epub is just a zip file

mkdir tempfolder
mv yourbook.epub tempfolder
cd tempfolder
unzip yourbook.epub
mv yourbook.epub ../youroldbook.epub

Find and edit the metadata in opf file

find . -iname '*.opf'

Metadata would most probably be in the <dc:title> tag,

<dc:title id="pub-title">TITLE OF YOUR BOOK</dc:title>

zip it after editing

zip -rX ../YOUR_BOOK.epub mimetype META-INF/ .
| improve this answer | |

Ok, so since nobody has ANY info on this online and this is the top search results. Here is what you are looking for. exiftool -a -u -g1 nameofthe.epub. This line gives you a giant output of all the metadata tags you can extract using exiftool. exiftool -t -Title -Creator -PublicationDate -CreatorFile-as nameofthe.epub this is how you use it. If you push -t lowercase it shows you the description of the tags you pulled. If you run -T uppercase, it just shows the tags. Your welcome. Enjoy.

Edit: Also, since exiftool is a GIANT PITA and their help is semi useless. Here is a few scripts I wrote for it that might help someone out. This took me forever... These are formatted for batch files with %% and ". You may need to edit

This moves any epubs in folder DIR into folders sorted by their Creator (Author).

for %%f in (DIR\*.epubs) do (exiftool "%%f" "-Directory<Creator")

This renames any epubs in a folder, to the Title and Author

for %%f in (01-input\*.epub) do (exiftool "-filename<$title - $creator.%%e" "%%f")

To make them recursive into subdirectories you need to change the for formatting. Example

for /R "01-input" %%f in (*.epub) do (exiftool "-filename<$title - $creator.%%e" "%%f")

Edit2: On the off chance your tags contain "illegal" characters, outlined here (https://exiftool.org/filename.html), place the tag inside {} and put a ; at the end of the tag. This strips those out so it works. Example script.

for /R "01-input" %%f in (*.epub) do (exiftool "-filename<${title;} - ${creator;}.%%e" "%%f")
| improve this answer | |

Expounding on @pheon's answer, I created this shell script



filename=$(basename "$1")
directory=$(dirname "$1")
newfilename=`${exiftool} -T -Title $1`

echo mv "$1" "${directory}/${newfilename}.${extension}"
mv "$1" "${directory}/${newfilename}.${extension}"

Just run the script, passing the pub filename and it will rename it in-place to be the title of the eBook. Note that it is likely there will be spaces in the filename.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.