1

This question already has an answer here:

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?

marked as duplicate by muru, Celada, slm Feb 16 '15 at 14:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

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). – 01AutoMonkey Feb 16 '15 at 13:13
  • Try $1 instead. – Josh Jolly Feb 16 '15 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 {} \; – steeldriver Feb 16 '15 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 '15 at 13:38
3

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 '15 at 12:58
  • @orion I edited. – Karthikeyan.R.S Feb 16 '15 at 13:00

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