Saturday, September 1, 2007

Shell script to rename set of files

For me the need to rename the files under unix is almost a frequent task,
like backup the set of unix files while some activity will overwrite the existing files or using the database creation template scripts to create another new database.
I use the following simple shell script to rename the files,

For example we have a set of files as follows,
/var/tmp/orainstall1.sql
/var/tmp/orainstall2.sql
/var/tmp/orainstall3.sql
/var/tmp/orainstall4.sql
/var/tmp/orainstall5.sql
and I want to rename them as
oracreate1.sql,
oracreate2.sql,
oracreate3.sql,
oracreate4.sql,
oracreate5.sql
I am listing the files under /var/tmp
$cd /var/tmp
$pwd
$/var/tmp
$ls ora*.sql
$orainstall1.sql
orainstall2.sql
orainstall3.sql
orainstall4.sql
orainstall5.sql
--script start here
$for file in orainstall*.sql;do
newfile=`echo $file | sed 's/install/create/'`
mv $file $newfile
done

--script end here
Checking the files for change
$pwd
/var/tmp
$ls ora*.sql
oracreate1.sql
oracreate2.sql
oracreate3.sql
oracreate4.sql
oracreate5.sql
$

Happy testing..

1 comment:

Anonymous said...

Good post.