Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I rename all *.txt in a directory to *.csv files in the terminal (Mavericks). I tried following inside the directory:

mv *.txt *.csv
share|improve this question
This can be done nicely in AppleScript. Then AppleScript can be called with the Terminal command osascript. – Nicolas Barbulesco Aug 29 '14 at 12:46

You need to loop over all files

for f in *.txt; do
    mv "$f" "${f%.txt}.csv"

If you have files with names starting with a . as well you might need to run

for f in *.txt .*.txt; do
    [[ -f "$f" ]] && mv "$f" "${f%.txt}.csv"
share|improve this answer
You can do it in the shell without calling an external program: mv "$f" "${f%.txt}.csv" – glenn jackman Aug 29 '14 at 12:00
This skips the files starting with . – Nicolas Barbulesco Aug 29 '14 at 13:00
@glennjackman Thanks. I always get the syntax wrong on that one, didn't want to post without having an Unix system to test it on. – patrix Aug 29 '14 at 13:04
@NicolasBarbulesco True, but given the special role .something files play in the shell I wouldn't recommend to start file names with a dot anyway. If you really have to, you can always run for f in .*.txt; do instead. – patrix Aug 29 '14 at 13:06
If you want to include dot files, shopt -s dotglob and the file expansion will pick them up. – glenn jackman Aug 29 '14 at 13:09

This also works well:

# change to the Zsh

# load the built-in zmv function
autoload -U zmv

# rename the files
zmv '(*).txt' '$1.csv'
share|improve this answer
But this skips the files starting with . – Nicolas Barbulesco Sep 3 '14 at 8:45
ls *.txt |awk '{print "mv "$1" " substr($1,1,(index($1,".txt")))"csv"}' |sh

Command explanation:

ls *.txt lists files ending with txt

| awk '{print "mv "$1" " substr($1,1,(index($1,".txt")))"csv"} prints the command that allows to rename the previous files listed with txt to csv

| sh executes the previously printed command, thus effectively renaming the files

Note: if you want to list all files including hidden files (those starting with .) use ls -a *.txt instead

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Care to break it apart (explain it piece by piece) for the non-technical users? – Alex Jan 10 at 10:29

Your Answer


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.