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
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
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'
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
If you have a huge number of files inside the directory, then even the ls*.txt command can fail, in that scenario, the below command can help:
find . -maxdepth 1 -name '*.txt' | sed 's/^\.\///g' | while read f1 ;do mv -v "$f1" "${f1%.*}.csv" ; done
osascript
.