Sign up ×
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.

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.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:… – Bernhard Jul 21 '12 at 17:30

2 Answers 2

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

find / -name "*.md" -type f -exec sh -c 'markdown "${0}" > "${}.html"' {} \;
share|improve this answer
Don't you get 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 ${}/.html part. – Abhijeet Rastogi Jul 22 '12 at 9:50
Btw, I just checked. you probably wrote forward slash by mistake in ${}/.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 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 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.