1831645 Members
1928 Online
110029 Solutions
New Discussion

Need some scripting help

 
SOLVED
Go to solution
wvsa
Regular Advisor

Need some scripting help

Greetings fellow admins. Does anyone have a script that makes a copy of a file. I will need to run this script as a cron job, so each time the script runs the output from the cp needs to go to a different file. Thankyou for your help. Oh yes can someone let me know with the new format to give points for answers, cant seem to find the option. Thankyou in advance.
4 REPLIES 4
Mark Grant
Honored Contributor

Re: Need some scripting help

Does the output from the copy need to be to a specific file name because there are quite a lot of possibilitie here.

For example

cp sourcefile $RANDOM
cp sourcefile newfile$(date +%d)
etc etc
Never preceed any demonstration with anything more predictive than "watch this"
Pete Randall
Outstanding Contributor
Solution

Re: Need some scripting help

You just need to put a timestamp on the output file:

export stamp=$(date +%y-%m-%d:%H:%M:%S)
cp file filenew.${stamp}

Points assignment should be via the pulldown menu next to each response, I believe.


Pete



Pete
Chris Vail
Honored Contributor

Re: Need some scripting help

Its fairly easy to use some form of versioning. Put the latest version number in a datafile:
DATAFILE=PATH/FILENAME
if test -s "$DATAFILE"
then
OLD_VERSION_NUM=`cat $DATAFILE`
else
OLD_VERSION_NUM="0"
fi
NEW_VERSION_NUM=`echo "$OLD_VERSION_NUM + 1"|bc`
echo "$NEW_VERSION_NUM">$DATAFILE
cp file file.$NEW_VERSION_NUM

This sets a default version of 1 if the data file is missing, and increments whatever is in the file if it isn't missing.
Steven Sim Kok Leong
Honored Contributor

Re: Need some scripting help

Hi,

Here's my personal preference:

cp $file $file.`date +%Y%m%d`.`date +%H%M`

An example of the created file is myfile.20031004.1018

Preferred this for ease of sorting. Hope this helps. Regards.

Steven Sim Kok Leong