Shell script to rename a group of files

Hello,

I am having 1800 files in a directory with a specified format, like

amms_850o_prod.000003uNy
amms_850o_prod.000003u8x
amms_850o_prod.000003taP
amms_850o_prod.000003tKy
amms_850o_prod.000003si4
amms_850o_prod.000003sTP
amms_850o_prod.000003sBg
amms_850o_prod.000003rvx
amms_850o_prod.000003re7
 
amms_810i_20091021.0510037
amms_810i_20091021.0740001
amms_810i_20091020.1540011
amms_810i_20091020.1710011
amms_810i_20091021.05100310
amms_810i_20091022.0510024
amms_810i_20091021.0510038
amms_810i_20091021.1940001
amms_810i_20091021.1810011
amms_810i_20091021.1440011
 
amms_856i_20100114.1710001
amms_856i_20100118.1210011
amms_856i_20100118.0510024
amms_856i_20100118.0510022
amms_856i_20100115.1810011
amms_856i_20100115.1610001
amms_856i_20100115.1540011
amms_856i_20100115.1410001
amms_856i_20100118.1540013
 
iabs_i_prod.20090921_044434
iabs_i_prod.20090918_042325
iabs_i_prod.20090917_042428
iabs_i_prod.20090916_042342
iabs_i_prod.20090914_042437
iabs_i_prod.20090930_050453
iabs_i_prod.20090929_050508
iabs_i_prod.20090923_040343
iabs_i_prod.20090925_044450
iabs_i_prod.20090928_042431
 
rps_i_prod.20091117165711
rps_i_prod.20091118014217
rps_i_prod.20091118165710
rps_i_prod.20091119021211
rps_i_prod.20091123044331
rps_i_prod.20091124021213
rps_i_prod.20091123170213
rps_i_prod.20091123180001
rps_i_prod.20091123014212
rps_i_prod.20091120021212

in all these file, i need to replace prod with test. As i am new to shell scripting can anyone helpe me in writing a script which does this renaming of files.

Regards,
Bharath.S

You only need to rename the files with "prod" in their names? (not all the "files" you listed have "prod" in their names).

ls *prod* | sed "p;s/prod/test/" | xargs -n 2 echo mv

mv amms_850o_prod.000003re7 amms_850o_test.000003re7
mv amms_850o_prod.000003rvx amms_850o_test.000003rvx
mv amms_850o_prod.000003sBg amms_850o_test.000003sBg
mv amms_850o_prod.000003sTP amms_850o_test.000003sTP
mv amms_850o_prod.000003si4 amms_850o_test.000003si4
mv amms_850o_prod.000003tKy amms_850o_test.000003tKy
mv amms_850o_prod.000003taP amms_850o_test.000003taP
mv amms_850o_prod.000003u8x amms_850o_test.000003u8x
mv amms_850o_prod.000003uNy amms_850o_test.000003uNy
mv iabs_i_prod.20090914_042437 iabs_i_test.20090914_042437
mv iabs_i_prod.20090916_042342 iabs_i_test.20090916_042342
mv iabs_i_prod.20090917_042428 iabs_i_test.20090917_042428
mv iabs_i_prod.20090918_042325 iabs_i_test.20090918_042325
mv iabs_i_prod.20090921_044434 iabs_i_test.20090921_044434
mv iabs_i_prod.20090923_040343 iabs_i_test.20090923_040343
mv iabs_i_prod.20090925_044450 iabs_i_test.20090925_044450
mv iabs_i_prod.20090928_042431 iabs_i_test.20090928_042431
mv iabs_i_prod.20090929_050508 iabs_i_test.20090929_050508
mv iabs_i_prod.20090930_050453 iabs_i_test.20090930_050453
mv rps_i_prod.20091117165711 rps_i_test.20091117165711
mv rps_i_prod.20091118014217 rps_i_test.20091118014217
mv rps_i_prod.20091118165710 rps_i_test.20091118165710
mv rps_i_prod.20091119021211 rps_i_test.20091119021211
mv rps_i_prod.20091120021212 rps_i_test.20091120021212
mv rps_i_prod.20091123014212 rps_i_test.20091123014212
mv rps_i_prod.20091123044331 rps_i_test.20091123044331
mv rps_i_prod.20091123170213 rps_i_test.20091123170213
mv rps_i_prod.20091123180001 rps_i_test.20091123180001
mv rps_i_prod.20091124021213 rps_i_test.20091124021213

(remove the echo before mv if that's what you want).

Or look at the rename command, if you have it.

Alternatively in bash or ksh93:

ls *prod*|while read i; do mv "$i" "${i/prod/test}"; done

Looks good.:b:

Thanks for your reply, But when i used the command
ls *prod*|while read i; do mv "$i" "${i/i_prod/t_test}"; done

its giving me the error

"ksh: "${i/i_prod/t_test}": bad substitution"

Can you help in correcting it...

I would assume you are using ksh88, not ksh93. What OS are you using?

I dont know how to find the version of my OS. can you tell me the command.

The question really was which OS (not which version of the OS).

In any case the original xargs solution I posted should work?

uname -a

when i execute that command, its gives me the result as
HP-UX hpomt73e B.11.11 U 9000/800 1623329386 unlimited-user license.

Hi Atlantis, that is HPUX 11i (v1) . You can use #!/usr/dt/bin/dtksh

Its not working even after adding the mentioned line also. so my script looks like

#!/usr/dt/bin/dtksh
ls *prod* | sed "p;s/i_prod/t_test/" | xargs -n 2 echo mv > out1

I thought you were going to try it with my line that you modified slightly, since there's where you got the bad substitution error? So:

#!/usr/dt/bin/dtksh
ls *prod* |
while read i; do 
  mv "$i" "${i/i_prod/t_test}"
done

What error did you get with scottn's code?