How to change name for N number of files

Hi Guys,

Please can you let me know, how to change file names in a directory that has say 10 files

I have files like

STR1.rman.pre.script
STR1.rman.post.script
STR1.rman.inc.script
STR1.rman.cold.script
STR1.rman.hot.script

I need a command to change the above files all in one go to

OPSW7.rman.pre.script
OPSW7.rman.post.script
OPSW7.rman.inc.script
OPSW7.rman.cold.script
OPSW7.rman.hot.script

Thanks in advance
G

#!/bin/ksh
for mFName in STR1*
do
  mFNameOut=`echo ${mFName} | sed 's/STR1/OPSW7/'`
  echo 'In = '${mFName}' Out = '${mFNameOut}
  mv ${mFName} ${mFNameOut}
done
for file in STR1.*
do
  mv "$file" "OPSW7${file#STR1}"
done


Hi, a one liner reusing cfajohnson code:

ls STR1.rman.* |xargs -i ksh -c 'file={};mv "$file" "OPSW7${file#STR1}"'

Regards.

This is my solution :slight_smile:

$ ls
STR1.rman.cold.script STR1.rman.inc.script STR1.rman.pre.script
STR1.rman.hot.scrip STR1.rman.post.script

$ ls | while read file
> do
> TOCH=`echo $file | awk -F . '{print $1}'`
> mv $file `echo $file | sed "s/^$TOCH./OPSW7./"`
> done

$ ls
OPSW7.rman.cold.script OPSW7.rman.inc.script OPSW7.rman.pre.script
OPSW7.rman.hot.scrip OPSW7.rman.post.script

Guys,

As an extention to the above question, if i were to have say 10 files in a directory and i wish to change a string called "Myapp" to "Mydb", how do i do that ?

Thanks very much

Regards
G

$ for ((i=0;i<=10;i++)); do touch Myapp_$i; done
$ ls
Myapp_0         Myapp_2         Myapp_5         Myapp_8
Myapp_1         Myapp_3         Myapp_6         Myapp_9
Myapp_10        Myapp_4         Myapp_7
$ ls Myapp* | while read file; do mv $file `echo $file | sed 's/Myapp/Mydb/'`; done
$ ls
Mydb_0  Mydb_10 Mydb_3  Mydb_5  Mydb_7  Mydb_9
Mydb_1  Mydb_2  Mydb_4  Mydb_6  Mydb_8

Cheers
ZB