hi,
I need some help in writing shell script in a bourne shell.I am trying to rename the file..
eg.
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,...
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.
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
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.
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
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.
#!/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