dyrobooks
Batch Convert Files with Pandoc
Many times, when I use Markdown, I work on one file. Once I’m done with it, I convert it to another format, such as a docx or html file. Occasionally, I have to create a small batch of files and convert them. Once I’m in a writing mode, I prefer to “stay in the flow” and write. Conversion can be performed later.
I use Pandoc to convert files and it’s possible convert the lot in one shot.
For this to work, Pandoc needs to be installed. Markdown can convert its files to .html, but if there’s a chance that you’ll have to convert files to other formats, Pandoc is the tool to use. For this post, I’ll assume that you have Pandoc installed.
In this example, I used a Mac and created the Markdown files in a directory called mph.
First, I created the Markdown files. To convert the files, we need to use the command line. Once in the mph directory folder, I ran the ls command to see the list of Markdown files I created.
bills-imac:mph bdyer$ ls -l *.md
-rw-r--r--@ 1 bdyer staff 1384 Jun 25 16:58 file1.md
-rw-r--r--@ 1 bdyer staff 2977 Jun 25 17:12 file2.md
-rw-r--r--@ 1 bdyer staff 1207 Jun 25 16:17 file3.md
-rw-r--r--@ 1 bdyer staff 2840 Jun 25 17:02 file4.md
-rw-r--r--@ 1 bdyer staff 3659 Jun 25 15:46 file5.md
-rw-r--r--@ 1 bdyer staff 4130 Jun 25 15:21 file6.md
-rw-r--r--@ 1 bdyer staff 2647 Jun 26 12:37 file7.md
-rw-r--r--@ 1 bdyer staff 427 Jun 25 15:11 file8.md
-rw-r--r--@ 1 bdyer staff 1069 Jun 25 15:47 file9.md
bills-imac:mph bdyer$
Note: Yes, I did change the names of the files for this post. It’s not that I’m being secretive; I figured that these examples would be easier to follow with the names changed.
To show that there are no HTML files yet…
bills-imac:mph bdyer$ ls -l *.html
ls: *.html: No such file or directory
Now we get to use Pandoc to do its magic on the collection of files. To do this, I run a one-line command that:
- calls pandoc
- reads the .md files and exports them as .html
The command also echoes the names of the .md files. The command is:
for i in *.md ; do echo "$i" && pandoc -s $i -o $i.html ; done
Here’s what the command line looks like after I’ve executed the command:
bills-imac:mph bdyer$ for i in *.md ; do echo "$i" && pandoc -s $i -o $i.html ; done
file1.md
file2.md
file3.md
file4.md
file5.md
file6.md
file7.md
file8.md
file9.md
bills-imac:mph bdyer$
To see if I have HTML files, I use the ls command to list them:
bills-imac:mph bdyer$ ls -l *.html
-rw-r--r-- 1 bdyer staff 2567 Jun 26 12:27 file1.html
-rw-r--r-- 1 bdyer staff 4190 Jun 26 12:27 file2.html
-rw-r--r-- 1 bdyer staff 1896 Jun 26 12:27 file3.html
-rw-r--r-- 1 bdyer staff 3608 Jun 26 12:27 file4.html
-rw-r--r--@ 1 bdyer staff 4603 Jun 26 12:27 file5.html
-rw-r--r-- 1 bdyer staff 5201 Jun 26 12:27 file6.html
-rw-r--r-- 1 bdyer staff 3209 Jun 26 12:27 file7.html
-rw-r--r-- 1 bdyer staff 999 Jun 26 12:27 file8.html
-rw-r--r-- 1 bdyer staff 1802 Jun 26 12:27 file9.html
bills-imac:mph bdyer$
For this particular job, I needed to create web pages, but we’re not limited to this. The same thing can be done if I need to convert to, say, .docx files. Simply run the above command, making sure to use $i.docx in place of $i.html in the command string.