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.

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 at 12:46

2 Answers 2

You need to loop over all files

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

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"
done
share|improve this answer
6  
You can do it in the shell without calling an external program: mv "$f" "${f%.txt}.csv" –  glenn jackman Aug 29 at 12:00
1  
This skips the files starting with . –  Nicolas Barbulesco Aug 29 at 13:00
1  
@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 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 at 13:06
2  
If you want to include dot files, shopt -s dotglob and the file expansion will pick them up. –  glenn jackman Aug 29 at 13:09

This also works well:

# change to the Zsh
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 at 8:45

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.