reddit / emacs

Bulk Converting Folder of Markdown files with spaces in the filename to Org-Mode format?

MethAddictedMonkey · 2022-06-19 11:33
I am trying to bulk convert an entire folder full of Markdown files into Org-Mode format. The issue is that the files are book titles from my Kindle notes with spaces in the file name. When the title is one single word the script below works well. But titles with spaces do no work. How do I modify the script below to convert a folder full of markdown files with spaces in the file name to org-mode using pandoc? for f in `ls *.md`; do pandoc -f markdown -t org -o ${f}.org ${f}; done
_viz_ · 2022-06-19 11:52
Use find(1) instead. find . -type f -iname '*.md' -exec pandoc -f markdown -t org -o {}.org {} \; Or don't even bother with the shell and its annoying quoting rules by using dired instead: mark the files, say ! (or &) then type the pandoc command replacing ${f} with \`?\` instead.
Zinjanthr0pus · 2022-06-19 12:23
Not to say that a shell `for` script is necessarily the best way to do it, but if going that way, you probably want to trim the `.md` off of the filenames (unless pandoc does this automatically). You can do this by replacing `"${f}.org"` with `"${f%%".md"}.org"`.
a-c-d-w · 2022-06-19 12:30
find(1) works, and the other suggestion for trimming the extension is good too. However, your original loop will work too, with some modifications (mostly quoting): for f in *.md; do pandoc -f markdown -t org -o "${f%%.md}.org" "$f" done
jsled · 2022-06-19 12:40
Just (double-)quote the file name variable. (This isn't an emacs question, but a shell quoting question.)
MethAddictedMonkey · 2022-06-19 13:37
>for f in \*.md; do pandoc -f markdown -t org -o "${f%%.md}.org" "$f" done Thanks! This worked perfectly for my use case. Just need to put it in a Bash script and run it once a day.
sarit-hadad-enjoyer · 2022-06-19 15:41
Iterating over `ls` output is a common bash pitfall, you can read more about it (and about the various workarounds) here: http://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29
noman_032018 · 2022-06-19 16:40
You can get some slightly cleaner extension-trimming via piping it through [GNU Parallel](https://www.gnu.org/software/parallel/) and somewhat faster finishing too. Something like: `find . -type f -iname '*.md' -print0 | parallel -0 pandoc -f markdown -t org -o {.}.org {}` Although simply using `Wdired` and `query-replace` to cleanup the names after using the dired-based command works just fine as well.