Renaming File Names in a folder/Dir

Hi Team,
I'm new to Unix shell scripting .
I've the following requirement

A folder contains the list of files with the following format

    ab.name.11.first
    ab.name.12.second
    ab.name.13.third 
    ----------
  

I have to rename the above file to like below

    box_name_11_first
    box_name_12_second
    box_name_13_third
    -------
  

Could you please give me some idea to rename the file (not the content in the file )

What shell are you using? Using bash or ksh93:

for i in ab.name.*
do
 new=${i//./_}
 new=${new/#ab/box}
 echo mv "$i" "$new"
done

Remove the echo when satisfied with the command lines.

Thank You for the reply elixir,
I'm using KSH

I'll test the same in my system and let you know

---------- Post updated 06-12-13 at 01:08 PM ---------- Previous update was 06-11-13 at 10:59 PM ----------

Hi Elixir,
I tried the above code which you have suggested .
I'm getting the following error

new=${i//./_}: bad substitution

Could you please suggest if i need to change any
Thank you

---------- Post updated at 02:33 PM ---------- Previous update was at 01:08 PM ----------

Could you please suggest me regarding this script

it works in ksh on my system...

Hi I tried like below

for i in ab.name.*
do
 new=${i//./_}
 new=${new/#ab/BOX}
 echo mv "$i" "$new"
done

And the error like below

$ ./test.sh
./test.sh[3]: new=${i//./_}: bad substitution

And it is just For Your Information , I'm trying to replace the FILENAMES and NOT the contents in a file

Thank You

You seem to be using ksh88 , not ksh93 .
Try:

for i in ab.name.*
do
 new=$(echo "$i"|sed 's/^ab/BOX/;s/\./_/g')
 echo mv "$i" "$new"
done
1 Like

try escaping the dot

 new=${i//\./_}
1 Like