Change all the .txt files to .md files in a certain folder

Is there a Terminal command or something like that to batch rename files in a certain folder? Basically I want to change all the files that have a .txt as a file type, and change it to .md (for markdown). Is this possible? I am comfortable using the Terminal, being a developer, so don't hold back on solutions. :)

Thanks in advance for any help you can provide.


Solution 1:

You can do the following in Terminal:

find . -iname "*.txt" -exec bash -c 'mv "$0" "${0%\.txt}.md"' {} \;

This will recursively rename all .txt files in the current directory to .md.

Solution 2:

Even if you do not use zsh as your default shell, you can still use zmv in a temporary shell:

zsh -c 'autoload zmv;$0 $@' zmv -w '*.txt' '$1.md'

Recursively:

zsh -c 'autoload zmv;$0 $@' zmv -w '**/*.txt' '$1$2.md'

Note: The -n option is handy for testing.

zmv is described in the zshcontrib manpage, and the full glob syntax is described in the zshexpn manpage in the “Filename Generation” section.