So I have

find . -name \*.md -type f -exec pandoc --filter ./filter1.py -o {}.html {}

And notice the {}.html. The {} returns a filename ending with .md, but I want it to just return the filename without the .md, so say I have a file named index.md, {} should return index, resulting in a file named index.html, rather then resulting in a filename named index.md.html.

So how would I go about removing the .md within this command?


2 Answers 2


It will work.

For example consider a .txt file in a current directory

find . -type f -iname "*.txt" -exec basename \{\} .txt \;
  • Be careful, it also strips away the directory.
    – orion
    Feb 16, 2015 at 12:58
  • @orion I edited. Feb 16, 2015 at 13:00

You can use -exec to create a new bash shell, then manipulate {} inside the shell by passing it as a parameter (it can be accessed as $0 in the new shell). You can remove the .md filetype ending with parameter expansion:

find . -name '*.md' -type f -exec bash -c 'pandoc --filter ./filter1.py -o ${0%md}html' {} \;
  • ${0} seems to return "bash" and not the filename, that is if I simplify your command down to: find . -name '*.md' -exec bash -c 'echo ${0}' \; I get echoed back "bash" three times (I have three .md files). Feb 16, 2015 at 13:13
  • Try $1 instead.
    – Josh Jolly
    Feb 16, 2015 at 13:31
  • @JoshJolly I think your original was correct - perhaps @01AutoMonkey missed the following {}? if you use $1 AFAIK you will need to add a 'dummy' positional parameter e.g. bash -c 'echo ${1%.md}.html' sh {} \; Feb 16, 2015 at 13:34
  • Ah yes, you're right @steeldriver. @01AutoMonkey - you need to have the {} after the bash shell command to pass it in as a parameter.
    – Josh Jolly
    Feb 16, 2015 at 13:38

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