How to change name for N number of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change name for N number of files
# 1  
Old 08-02-2007
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
# 2  
Old 08-02-2007
Code:
#!/bin/ksh
for mFName in STR1*
do
  mFNameOut=`echo ${mFName} | sed 's/STR1/OPSW7/'`
  echo 'In = '${mFName}' Out = '${mFNameOut}
  mv ${mFName} ${mFNameOut}
done

# 3  
Old 08-03-2007
Quote:
Originally Posted by kamathg
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
Code:
for file in STR1.*
do
  mv "$file" "OPSW7${file#STR1}"
done

# 4  
Old 08-03-2007
Hi, a one liner reusing cfajohnson code:

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

Regards.
# 5  
Old 08-03-2007
How to change name for N number of files

This is my solution :-)

$ 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
# 6  
Old 08-08-2007
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
# 7  
Old 08-08-2007
Code:
$ 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (7 Replies)
Discussion started by: ust3
7 Replies

3. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (0 Replies)
Discussion started by: ust3
0 Replies

4. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

5. UNIX for Dummies Questions & Answers

How do i change string in a certain column number?

I have a file which looks like this 01081023442 220000000410629994812279000001099952504280000000000000000000000000010050000000000 0000000000 0000000000 1101211221099952504280000010704525042800120000000000000000000000000000000000001 0000000000000006000000000000815500000010000010000000000000... (2 Replies)
Discussion started by: reignangel2003
2 Replies

6. Shell Programming and Scripting

change number into dollars and cents

in ksh. Here is what I have working thanks to all of you. Now I need to take it one step further. The $result value needs to be in the $0000.00 format instead of what it is now, 000000. Yes, it is always 6 characters, I thought this would help but so far it has baffled me. My thought... (3 Replies)
Discussion started by: CougarMutt
3 Replies

7. Shell Programming and Scripting

Change the File name using pattern and number

Hi, i have the below files in the wrk folder. File names are AAB0001.R BBA0002.R CCDC0003.R AAB0002.R AAB0034.R BBA0081.R DDDE0008.R i need to change the file name USING pattern and number. output like below :wall: AAB0001.R AAB0002.R AAB0003.R BBA0001.R BBA0002.R CCDC0001.R... (3 Replies)
Discussion started by: krbala1985
3 Replies

8. SCO

Why? I can not change the number of lines to print

hi My problem now is that if shipping options as -o length = 88 it says the following: # lp -o length=88 -dhp4015 /etc/hosts UX:lp: ERROR: The following options can't be handled: -o length= TO FIX: The printer(s) that otherwise qualify for printing your request can't handle one or more of... (2 Replies)
Discussion started by: Edgar Guevara
2 Replies

9. Shell Programming and Scripting

Need to change format of number

Hi, using a shell script to get values from a CSV eg: 12345.67,5678990.89,76232882.90 12345,5678990.89,76232882 Need the format of these numbers to change to 12,345.67:5,678,990.89:76,232,882.90 12,345:5678990.89:76232882 Using nawk on solaris, to parse these values, need the... (10 Replies)
Discussion started by: pgop
10 Replies

10. Shell Programming and Scripting

how to change a number in a file with sed?

Hello, I have a file which contains some line as follows, 9.9 TEMP 9.9 MUCOEFF 0.0 EPSILON And I want to increase this MUCOEFF by 0.2 as 9.9 TEMP 10.1 MUCOEFF 0.0 ... (7 Replies)
Discussion started by: lorenzz
7 Replies
Login or Register to Ask a Question