How to change extension?

How do you write a shell script that change the extension of all the files?

e.g

chext rtf doc

where .rtf is the original extension
and .doc is the new extension

is it something to do with basename?
do I need a for loop?
Please help!

Unix SuperNewbie

you will realize that making a search on these forums would give you more ideas... for exampe I searched for "rename files" and I got this link...

I have just modified a small piece of code from the above link...

Cheers!
Vishnu.

I can get the script working as follows:

#!/bin/sh
for name in `ls *.rtf`
do
name1=` echo $name| cut -f 1 -d . `
mv $name1.rtf $name1.doc
done

is working fine, but how do I make it work like this:

chext 1 2

where 1 is the original extension and 2 is the desire new extension?

replace those "rtf" and "doc" with $1 and $2 in your script...

I should add that the above way using "cut" will not work if you have multiple dots in your filename...

#!/bin/sh 
for name in `ls *.$1` 
do 
name1=`echo $name | sed -e "s/^\(.*\)\.$1$/\1\.$2/g"` 
mv $name $name1 
done

or a more compact and faster version which I prefer...

#!/bin/sh 
ls *.$1 | sed -e "s/^\(.*\)\.$1$/\1\.$1 \1\.$2/g" | xargs -n 2 mv -f

Cheers!
Vishnu.

thanks vishnu!!