Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
Awesome! Thanks a lot. :) –  Greduan Oct 5 '12 at 17:36
    
I tested it and it works, thanks a bunch. :) –  Greduan Oct 5 '12 at 17:37

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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