4

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.

15

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.

1

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.

  • This does seem like a shorter syntax, umm... Could you give me a little bit of what this zmv or zsh command does? Thanks. :) – greduan Oct 6 '12 at 12:55
  • With -w, each wildcard gets put into a positional parameter ($1, $2, etc.) to be used in the new name; without -w you must include parentheses to delimit the extent of the text that is matches and placed in the positional parameters. So, not using -w may make it a bit more obvious: zsh -c 'autoload zmv;$0 $@' zmv -n '(*).txt' '$1.md' renames all *.txt files (in the current directory) to end in .md instead of .txt; zsh -c 'autoload zmv;$0 $@' zmv -n '(**/)(*).txt' '$1$2.md' does likewise in the current directory and all subdirectories. – Chris Johnsen Oct 6 '12 at 20:00
  • Oh OK! Well thanks a lot! I used the other one but will keep this in mind for the future. :) – greduan Oct 8 '12 at 2:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .