Visit Our UNIX and Linux User Community


write shell script to rename file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting write shell script to rename file
# 1  
Old 12-09-2010
write shell script to rename file

hi,
I need some help in writing shell script in a bourne shell.I am trying to rename the file..
eg.
Code:
find /root/data -type f -name "text*) | while read FILES
do
  newfile=${FILES/type_2.0_20101208_34.xml / tmp.xml}
  mv "$FILES" "$newfile"
done

above written script is working...If the name of my file is changed into type_2.0_20101208_35.xml so i have to write this filename in script.Every time when file name is change i have to change the the file name in script..But i do not want to editing in script to change the name of file..so what i write in place of type_2.0_20101208_34.xml in code..
Please help me to overcome this problem,...

Last edited by Franklin52; 12-09-2010 at 07:17 AM.. Reason: Please use code tags and indent your code, thanks
# 2  
Old 12-09-2010
Have a counter defined and call it in the variable newfile
Code:
count=0
find /root/data -type f -name "text*" | while read FILES
do
	count=$(( ${count} +1 )) # shorter one (( count += 1 ))
	newfile=${FILES/type_2.0_20101208_${count}.xml/tmp.xml}
	mv "$FILES" "$newfile"
done

However i believe type_2.0_20101208_34.xml is directory in this case (FILES/type_2.0_20101208_34.xml/tmp.xml)

Last edited by michaelrozar17; 12-09-2010 at 07:55 AM..
# 3  
Old 12-10-2010
Quote:
Originally Posted by michaelrozar17
Have a counter defined and call it in the variable newfile
Code:
count=0
find /root/data -type f -name "text*" | while read FILES
do
    count=$(( ${count} +1 )) # shorter one (( count += 1 ))
    newfile=${FILES/type_2.0_20101208_${count}.xml/tmp.xml}
    mv "$FILES" "$newfile"
done

However i believe type_2.0_20101208_34.xml is directory in this case (FILES/type_2.0_20101208_34.xml/tmp.xml)

Thanks..I tried to run this code but i found error in mv command like
mv:can not stat `:no such file or directory

one thing i write to forget i have to also change the date with number.It means if the name of the file is type_2.0_20101208_34. if any modification is done in this file then next time this file is saved named as type_2.0_20101209_35.
# 4  
Old 12-10-2010
Quote:
Originally Posted by shubhig15
Thanks..I tried to run this code but i found error in mv command like
mv:can not stat `:no such file or directory
As stated earlier, every time it loops through the while condition the move command checks for the sub-directory FILES/type_2.0_20101208_* and copies the file to tmp.xml, since you have given the path as FILES/type_2.0_20101208_${count}.xml/tmp.xml. So unless you have the sub-directory type_2.0_20101208_* created under directory FILES you will have this error.
Guess you are aware of the directory-tree structure, where except tmp.xml all the others are directories and sub-directories here..FILES/type_2.0_20101208_${count}.xml/tmp.xml
This User Gave Thanks to michaelrozar17 For This Post:
# 5  
Old 12-10-2010
Quote:
Originally Posted by michaelrozar17
As stated earlier, every time it loops through the while condition the move command checks for the sub-directory FILES/type_2.0_20101208_* and copies the file to tmp.xml, since you have given the path as FILES/type_2.0_20101208_${count}.xml/tmp.xml. So unless you have the sub-directory type_2.0_20101208_* created under directory FILES you will have this error.
Guess you are aware of the directory-tree structure, where except tmp.xml all the others are directories and sub-directories here..FILES/type_2.0_20101208_${count}.xml/tmp.xml


how to overcome this type of error.
# 6  
Old 12-10-2010
It depends upon your requirement. Wot exactly your trying to do.. i mean from where to where your trying to move the files and wot is the destination file name for the move command. Is it always tmp.xml? Brief the requirements.
# 7  
Old 12-10-2010
Quote:
Originally Posted by michaelrozar17
It depends upon your requirement. Wot exactly your trying to do.. i mean from where to where your trying to move the files and wot is the destination file name for the move command. Is it always tmp.xml? Brief the requirements.

FROMDIR=/root/test/data
TODIR=/root/test/data1

data directory contains file tmp_2.0_20101208_34.xml and data1 contains file tmp.xml.I want to move tmp_2.0_20101208_34.xml file into data1 folder and after that its name replace with tmp.xml because other application using this tmp.xml.If user do any modification in this tmp_2.0_20101208_34.xml file than he save the name of the file as tmp_2.0_20101209_35.xml again this file is moved in data1 folder and name changed as tmp.xml.Destination file name is always tmp.xml

Previous Thread | Next Thread
Test Your Knowledge in Computers #819
Difficulty: Medium
In CSS, E ~ F matches an F element preceded by an E element.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help required with a file rename shell script

Hello everyone, Posting here after a long time, been away from unix world lately and it seems I have forgotten my shell scripting completely. I have a requirement where a csv file contains following columns: Full Registration VIN Stock... (14 Replies)
Discussion started by: tayyabq8
14 Replies

2. Shell Programming and Scripting

Need help to write a shell script to convert text file to excel file.

Hi Everyone, I want your help to write a script which will take text file as input and on the basis of delimiter ":"script will create excel sheet. Example input: IpAdress:InstanceName:Port:ServerName 10.255.255.1:abc:2232:xyz_abc Output should be an excel sheet like below: Column... (8 Replies)
Discussion started by: akabhinav18
8 Replies

3. Shell Programming and Scripting

How to write rename script?

I have files fd01.calc,fd02.calc,fd03.calc..I want to rename in fd01.picks,fd02.picks,fd03.picks How to do this? (4 Replies)
Discussion started by: mikibelavista
4 Replies

4. Shell Programming and Scripting

How can I write the specific content in the file through shell script

Hello, I need to do one thing that my script creates the file touch release.SPLASH_12_03_00_RC01.txt Now I want to update that file with some content e.g splashbuild::SPLASH_12_17_00_RC02.zip Thanks (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

5. Shell Programming and Scripting

how to write multiple lines to a file using shell script?

I need to create an xml using shell script, but i first want to know how can i write multiple lines to file using shell script? (7 Replies)
Discussion started by: vel4ever
7 Replies

6. Shell Programming and Scripting

Write output to a file using Korn shell script

All, Can anyone please help me with the below scenario in korn shell script. Can anyone please give me some hints to proceed on this. I have a Flat file of the below format. Input file format:... (1 Reply)
Discussion started by: sp999
1 Replies

7. Shell Programming and Scripting

write shell script to search file in folder

hi .. i have a problem how to search file with date and version number(ms_2.0_dd/mm/yy_24)in folder.Here 24 is version number and compare the this file to other file which is in another folder and if does not match then copy this file to respective folder.Also copy different files in different... (1 Reply)
Discussion started by: shubhig15
1 Replies

8. UNIX for Advanced & Expert Users

Rename a file to a file_current datetime in a shell script

Hi all, Could anyone suggest me on Renaming a file to a file_current datetime in a shell script. (3 Replies)
Discussion started by: Nithin
3 Replies

9. Shell Programming and Scripting

How to write a shell script to notify when certain texts appear in a file?

I have a server and occasionally the file mysqld.log would show something like /usr/libexec/mysqld: Disk is full writing './example_com_-_wordpress/wp_statpress.MYD' (Errcode: 122). Waiting for someone to free space... Retry in 60 secs How do I write a simple shell script to check mysqld.log... (1 Reply)
Discussion started by: acider
1 Replies

10. UNIX for Dummies Questions & Answers

Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename. Hello. I have files in folder. Like: xxxxxxxx1.html or xxxxxxxx2.txt or xxxxxxxx3.tar.gz and how to rename or change file extension case to xxxxxxxx1.htm or xxxxxxx2.TXT or (5 Replies)
Discussion started by: Sheldon
5 Replies

Featured Tech Videos