1752726 Members
5514 Online
108789 Solutions
New Discussion юеВ

Re: Help with scripting

 
Sean OB_1
Honored Contributor

Help with scripting

Hey scripting gurus.

I'd like some help in scripting the following.

A daily job that will analyze the files in a file system and then move off the files created the previous day to a heirarchical directory structure.

Where it gets a bit difficult is that they want to move it to a directory structure such as:

/Year/Month
/Year/Month/End Of Month

ex:

/Year/January
/Year/Januar/End Of Month
/Year/February
/Year/February/End of Month

So all files for the month, except the last day of the month get moved into the month directory. The files for the last day of the month get moved into the month/end of month directory.

The script will have to create the directories if they don't exist at the time the script is run. (first day of new month).

TIA and points for all responses.

Sean
5 REPLIES 5
Geoff Wild
Honored Contributor

Re: Help with scripting

First part like this:

MONTH=`date +%B`
mkdir /someotherdir/$MONTH

TODAY=`date +%b" "%d`

for FILE in `ll /somedir | grep -v "$TODAY" |awk '{print $9}`
do
mv $FILE /someotherdir/$MONTH
done

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: Help with scripting

No matter what you are told the problem as stated cannot be done for the simple reason that time of creation is not known. If you do know it, you only know it by accident if the modification or change time (these are not the same) happen to also be the time of creation. Creation time metadata simply don't exist in UNIX.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Help with scripting

Hi Sean:

It's easy to determine the first and last day of any month (including leap year) by simply comparing the current day to a predefined array of values (and adding one to February if the year is evenly divisible by four).

Given that, you can trigger a 'mkdir' to create any backup directory with the appropriate name.

Then, you might let your script 'touch' a "reference" file for the LAST day of the month thusly:

# touch -amt 11302359 /tmp/myref

Now, use 'find' to move everything but the last day to the month's directory:

# find /sourcedir -type f ! -newer /tmp/myref -exec mv {} /destdir \;

To move the last of the month's files to the end-of-month directory, simply drop the negatation ( the bang synbol, "!" ):

# find /sourcedir -type f -newer /tmp/myref -exec mv {} /destdir \;

Regards!

...JRF...
Geoff Wild
Honored Contributor

Re: Help with scripting

Here's how you can get the last day of the current month:

set -- `cal ` ; eval LDAY=\${$#}
echo $LDAY

CURMONTH=`date +%b`
Then, grep for "$CURMONTH $LDAY"

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Howard Marshall
Regular Advisor

Re: Help with scripting

The way you have phrased this question I am assuming that some process or processes are creating these files and not people and you are just archiving them.

No problem, couldn't be simpler.

Set up a cron job to run every day a few minuets after midnight.

Then based on the current date decide what your subdirectory is going to be and move the files with a modification data of greater than today

#!/usr/bin/ksh

date -u +%E" "%B" "%d | read YEAR MONTH DAY

LASTMONTH="you figure it out hint, case statment"

SUBDIR=${YEAR}/${MONTH}

# these work like if thens but simpler to read
# if the first test case succeeds the && makes shell run the next
# if the first test case fails it doesn't bother running
# the next test case sense the entire line has already failed
[ $MONTH = "Janruary" ] && [ $DAY = 1 ] && YEAR=$(expr ${YEAR} - 1)
[ $DAY = 1 ] && SUBDIR=${YEAR}/${LASTMONTH}/eom
[ ! -d $SUBDIR ] && mkdir -p $SUBDIR

find . -mtime +1 -exec mv {}";" $SUBDIR


I didn't test that at all so you may have to make a few tweeks here and there, or heck just rewrite it to suit your self. That├в s just the basic idea though. Moving the files is the easy part, deciding where to move them is more complicated.

H