Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Lets suppose I have files on my harddisk having extensions as .md.

I want to convert all those files to .html through find and the -exec option.

The command to convert a markdown file to html is

markdown readme.md > readme.html

I am trying to use something like:

find / -name "*.md" -type f -exec markdown {} > {}.html \;

Of course, the above doesn't work. So, how do I do that using only find and exec?

share|improve this question
Related: unix.stackexchange.com/questions/39654/… – Bernhard Jul 21 '12 at 17:30
up vote 14 down vote accepted

You need to quote around the construct containing > because it's a shell special character, like this:

find / -name "*.md" -type f -exec sh -c 'markdown "$0" > "$0.html"' {} \;

This will also rename the files so you end up with foo.html instead of foo.md.html:

find / -name "*.md" -type f -exec sh -c 'markdown "${0}" > "${0%.md}.html"' {} \;
share|improve this answer
Don't you get file.md.html now? Instead of file.html? – Bernhard Jul 21 '12 at 17:42
Yep, you're right. Fixed. Thank you. – rush Jul 21 '12 at 19:07
@rush Why do I need {} at the end in both commands? Also, in the second command, it's hard to follow for me as to what happened? I didn't understand the ${0%.md}/.html part. – Abhijeet Rastogi Jul 22 '12 at 9:50
Btw, I just checked. you probably wrote forward slash by mistake in ${0%.md}/.html. – Abhijeet Rastogi Jul 22 '12 at 10:00
@shadyabhi The final {} is the input argument for the sh -c string, and is there accessed as $0. The command you don't understand strips .md and adds .html. You're probably right with escaping the . – Bernhard Jul 22 '12 at 10:37

The way to achieve exactly what you have written is to use markdown's -o option, which specifies the name of the output file, i.e.

find / -name "*.md" -type f -exec markdown -o {}.html {} \;

Doing it this way avoids starting a new shell just to run markdown.

Unfortunately, as Bernhard points out, doing it this way gives you files named file.md.html instead of file.html, so rush's approach is more appropriate here.

share|improve this answer
But, markdown doesn't have a -o option. I double-checked before commenting. – Abhijeet Rastogi Jul 22 '12 at 16:58
markdown(1) says it does, but maybe you have a different version? – Mikel Jul 22 '12 at 18:36
You could also use pandoc -f markdown -t html -o.... – Mikel Jul 22 '12 at 18:37
Ohk, I was using daringfireball.net/projects/markdown and it doesn't have that. – Abhijeet Rastogi Jul 22 '12 at 19:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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