write shell script to rename file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting write shell script to rename file
# 8  
Old 12-10-2010
Try if the below code helps. Before running this script create a empty file in name prev_file.txt in the location where this script is placed.

Code:
#!/bin/ksh
# Saving as move_file.sh

FROMDIR=/root/test/data
TODIR=/root/test/data1
PREV_FILENAME=$(sed '1!d' prev_file.txt) # get the 1st line of the prev_file.txt
CUR_FILENAME=$(ls -tr /root/test/data/tmp_2.0*.xml | tail -1) # get the latest tmp_2.0* file

if [ "${PREV_FILENAME}" != "${CUR_FILENAME}" ] # move if previous and latest file are not the same. If not same the user has changed the file
then
	mv ${FROMDIR}/${CUR_FILENAME} ${TODIR}/tmp.xml 2>error_file.txt
	echo "Moved ${FILENAME} to tmp.xml"
	echo "${CUR_FILENAME}" > prev_file.txt # store current file name in a file to check if the same file exits in the next consecutive run
else
        echo "File not changed by the user"
fi

This User Gave Thanks to michaelrozar17 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question