Two years ago, I wrote an article about how to create an ebook in Open Office. Since then, I've moved to creating ebooks using the linux command line because I found it quicker and a lot simpler: from a finished text, a Kindle ebook can be created in less than a minute - and its all completely free. The tools I use to create epub and Kindle ready books using the command line interface (CLI) are any text editor (emacs, nano, vi, etc), pandoc and KindleGen.

Create the ebook

The most time consuming part (and fun part too) of the whole process is creating your ebook. To do this, you can use any text editor - you don't need any special software for this stage. You simply write your book using markdown syntax. Markdown allows you to write your book with the minimum of tags which can then be converted into lots of different formats. (This whole website is written in markdown.) An example of an ebook written on the Linux command line is shown below:

% The name of your book
% Your name

A statement to appear on the first page.

# Chapter One

Whatever you want to put into your first chapter.

# Chapter Two

The contents of the all important chapter two, which also contains an image

![A lovely image](images/lovely.jpg)

(For more details on the different formatting you can use in markdown then a markdown cheat sheet is invaluable.

Save this file as

You don't need to create a table of contents for your ebook at this point because pandoc will do this for you. When you've written your book, you're ready for the next stage of the process.

Convert markdown to epub format

The second stage of the process is to convert your markdown ebook to the epub format. For this, we'll need to install pandoc - the Swiss army knife of document conversion.

Download and install pandoc using:

sudo apt-get install pandoc

To create a simple ebook, make sure you're inside the correct directory and type:

pandoc -o mybook.epub

This will create an epub file for you which you can read on most ebook readers (but not on Kindles as they have their own format - we'll come to this later). Note that if your markdown file contains links to local images, for example:

![A lovely image](images/lovely.jpg)

then pandoc will automatically include the images in the generated epub.

Adding a cover to your ebook

To add a cover to your epub book then use the --epub-cover-image=cover.jpg flag. For example:

pandoc -o --epub-cover-image=cover.jpg myebook.epub

Adding a table of contents to your ebook

To add a table of contents to your epub, then add the --toc flag to the statement. For example:

pandoc -o --toc myebook.epub

Creating one ebook from multiple documents

If you're writing a large book, you may want to have one file per chapter. For example, This will make your work a lot easier to manage. Also, you may want to have extra information about the book included as a seperate document. For example a file called title.txt can contain the following:

% The name of your book
% Your name

To add all the chapters and the extra information together to create one book, to include an image for the title page and to create a table of contents, then the following command can be used:

pandoc -o mybook.epub --epub-cover-image=cover.jpg --smart --toc --epub-metadata=metadata.xml title.txt

Create a file compatible with the Kindle

If you want to sell your book on Amazon, or read it on your Kindle then you'll need to convert the file from the epub format to a format that the Kindle can recognise. Luckily, Amazon have made their KindleGen tool readily available for free download.

Convert your epub file to Kindle format

  1. Download the KindleGen tar.gz from to a folder such as Kindlegen in home directory (~/KindleGen).
  2. Extract the contents of the file to ~/KindleGen. Open the terminal, move to folder containing the downloaded file using command cd ~/KindleGen and then use command tar xvfz kindlegen_linux_2.6_i386_v2.tar.gz to extract the contents.
  3. Move kindlegen to /usr/local/bin by typing sudo mv kindlegen /usr/local/bin
  4. Go back to the directory that you created the epub files in and type in kindlegen mybook.epub which will create a file called in your current directory.

You now have a mobi file that can be uploaded to Amazon's Kindle Direct Publishing. Congratulations - you've published your ebook using free tools from the Linux command line!

As you can see, if you're creating a lot of books then this process is a lot faster and cheaper than other methods out there. I hope you've enjoyed reading this as much as I've enjoyed creating it.


comments powered by Disqus