Shell script to rename a group of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to rename a group of files
# 1  
Old 03-10-2010
Shell script to rename a group of files

Hello,

I am having 1800 files in a directory with a specified format, like
Code:
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

Last edited by Scott; 03-10-2010 at 05:32 PM.. Reason: Code tags please...
# 2  
Old 03-10-2010
You only need to rename the files with "prod" in their names? (not all the "files" you listed have "prod" in their names).

Code:
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.

Quote:
RENAME(1) Linux Programmer's Manual RENAME(1)

NAME
rename - Rename files

SYNOPSIS
rename from to file...

DESCRIPTION
rename will rename the specified files by replacing the first occurrence of from in their name by to.

For example, given the files foo1, ..., foo9, foo10, ..., foo278, the commands

rename foo foo0 foo?
rename foo foo0 foo??

will turn them into foo001, ..., foo009, foo010, ..., foo278.
# 3  
Old 03-10-2010
Alternatively in bash or ksh93:
Code:
ls *prod*|while read i; do mv "$i" "${i/prod/test}"; done


Last edited by Scrutinizer; 03-10-2010 at 10:02 PM..
# 4  
Old 03-11-2010
Quote:
Originally Posted by Scrutinizer
Alternatively in bash or ksh93:
Code:
ls *prod*|while read i; do mv "$i" "${i/prod/test}"; done

Looks good.Smilie
# 5  
Old 03-17-2010
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...
# 6  
Old 03-17-2010
I would assume you are using ksh88, not ksh93. What OS are you using?
# 7  
Old 03-17-2010
I dont know how to find the version of my OS. can you tell me the command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Oop to copy and rename files through SQL Statement in shell Script

#!/bin/sh sqlplus -s "/ as sysdba" << EOF SET HEADING OFF SET FEEDBACK OFF Select pt.user_concurrent_program_name , OUTFILE_NAME FROm apps.fnd_concurrent_programs_tl pt, apps.fnd_concurrent_requests f where pt.concurrent_program_id = f.concurrent_program_id and pt.application_id =... (1 Reply)
Discussion started by: usman_oracle
1 Replies

2. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

3. Shell Programming and Scripting

Windows Power Shell - rename files and move

hi people; i want to make a file/folder operation as follows. - i have 41 folders in Windows and each of them have same-named files (~200 files each) inside. - i want to stack these files together in a folder but Windows is asking to "overwrite" (as usual) since the file names are the same.... (2 Replies)
Discussion started by: gc_sw
2 Replies

4. Shell Programming and Scripting

Need help for a Shell script to rename multiple files

Hi! I need help to create a shell script to search inside a file and then copy a portion of the search result as the new file name. Basically I was hacked over the weekend and the genius wipe out my drive from my server. I was able to recover alot of files, but biggest problem Is now the... (15 Replies)
Discussion started by: kidney514
15 Replies

5. Shell Programming and Scripting

Script to rename a group of files

Hello, I have a directory which has the file names as ap_boise_20091109.Z.20091110 ap_aero_20091020.Z.20091021 . .. ... I have to remove the time stamp after which is present after Z. as there are thousands of file to do so, is there a simple way to rename those file in a single... (4 Replies)
Discussion started by: atlantis_yy
4 Replies

6. Shell Programming and Scripting

how to rename all files that have a certain text in the filename using tcsh shell

Hello~ I'm on AIX version 5 and I believe I have the tcsh shell environment to play in. Can you guys help me with a solution to rename all files that have "eclp" in the filename to "ecl" ? I basically want to rename the files and strip the "p" out. i.e. original filenames: ... (3 Replies)
Discussion started by: in2vtec
3 Replies

7. Shell Programming and Scripting

Shell Script to rename files

Hi, i need a bit of help writting a tcsh script which renames all ascii text files in the current directory by adding a number to their names before the extension so for example, a directory containing the files Hello.txt Hello.t Hello should have the following changes, Hello.txt... (2 Replies)
Discussion started by: yakuzaa
2 Replies

8. Shell Programming and Scripting

Shell script to rename files with .1,.2,.3 ....ext respectively

Hey Guys.... Just need some help as I am not proficient in Unix shell script... Doubt: --------------- Suppose there will be some of the following files inside a directory called OUT ... Path: - /appdb1/product/batch/rms/OUT files inside OUT directory:- POSU_75002_20090127_20090129035442... (4 Replies)
Discussion started by: satyajit007
4 Replies

9. Shell Programming and Scripting

How to Rename/Convert Files in Shell Scripting?

Hi All, I want to Rename/Convert all the .doc files in to .pdf format. I am using the following Script. But the final output is not proper. ########################################## cd /u13/prepaid/ftpdata/INfiles/sap/ for name in `ls *.doc` do name1=`echo $name | sed -e... (11 Replies)
Discussion started by: hanu_oracle
11 Replies

10. Shell Programming and Scripting

rename files using shell scripting

Hi all, i want to rename some files in my directory using korn shell scripting. 1) i want to rename files who have no extension so that they will have the format: filename.extension and 2) i want the files who has extension initially, to stay the same (they will not be... (4 Replies)
Discussion started by: gfhgfnhhn
4 Replies
Login or Register to Ask a Question