KSH: How to move files from one direcotry to a different directory with condition check?

Hello all,
I have a directory called /apps/logs/sct/{$YEAR}/{$MONTH}. I have the following ksh script which check for year and month directory and copies several files from different directory to /{$YEAR}/{$MONTH}

#!/bin/ksh

YEAR=`date '+%Y'`
MONTH=`date '+%y-%B'`
DAY=`date '+%d'`
BASE_DIR=/apps/logs/sct
cd ${BASE_DIR}

   if [  -e  ${YEAR} ]
   then
      if [ ! -e ${MONTH} ]
      then
       mkdir ${YEAR}/${MONTH}
      fi
   else
     mkdir -p ${YEAR}/${MONTH}
   fi

# now move the files into the directory.
cp *.---.---.com  /apps/logs/sct/${YEAR}/${MONTH}

cp command is the only command that I know to use to copy the files into the /${YEAR}/${MONTH} directory. Is there any better command to use to be able to have condition check for current month before it copied that file into /${YEAR}/${MONTH}?
Please be specified,
Thanks,
Leyla
leylakhavaninAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amit_gCommented:
Do you have month in the file name? If yes you could use the cp command as ...

cp *.---.${MONTH}.com  /apps/logs/sct/${YEAR}/${MONTH}

If not. What is the criteria?
leylakhavaninAuthor Commented:
Yes. Here is the format of each file:
 
 2005-Jun-30-niclinux.pd9.---.com
 2005-Jun-30-ecs47m1.pd9.---.com
 2005-Jul-1-ecs44h1.pd9.---.com
 2005-Jul-1-batch3.pd9.---.com
 2005-Jul-1-batch2.pd9.---.com
 2005-Jul-1-batch1.pd9.---.com
 2005-Jul-1-batch.pd9.---.com

I want to check and make sure that the month of July's file will go to July direcotry and month of June's will go to June directory when I move files from /apps/logs/sct/ to /apps/logs/sct/${YEAR}/${MONTH}
amit_gCommented:
This should do it ...

FILE_PATTERN=`date '+%Y-%b'`

cp ${FILE_PATTERN}*.com  /apps/logs/sct/${YEAR}/${MONTH}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

leylakhavaninAuthor Commented:
amit,
That works fine, thank you :)
But, is there any way to copy the files that are still in month of Jun but after 28? In a simple way, a condition that can check
if the file date is anything after Jun-28 then send them to month of July.
Because some of our cron are set up to run few days before the end of the month.
amit_gCommented:
A better way would be to name the files correctly.

YEAR=`date '+%Y'`
PREVIOUS_MONTH=$((`date '+%m'` - 1))
DAY=`date '+%d'`

PREVIOUS_MONTH_PATTERN=`date -d ${YEAR}/${PREVIOUS_MONTH}/${DAY} '+%Y-%b'`
THIS_MONTH_PATTERN=`date '+%Y-%b'`

for i in ${PREVIOUS_MONTH_PATTERN}*.com
do
        if [ `echo $i | cut -d"-" -f3` -ge 28 ]
        then
                echo cp $i /apps/logs/sct/${YEAR}/${MONTH}
        fi
done

for i in ${THIS_MONTH_PATTERN}*.com
do
        if [ `echo $i | cut -d"-" -f3` -lt 28 ]
        then
                echo cp $i /apps/logs/sct/${YEAR}/${MONTH}
        fi
done
amit_gCommented:
Change echo cp to cp when you see the correct statements being echoed.
ahoffmannCommented:
> But, is there any way to copy the files that are still in month of Jun but after 28?

/apps/logs/sct/2005-Jun-{[1-9],1[0-9],2[0-8]}-Jun* /apps/logs/sct/${YEAR}/June
/apps/logs/sct/2005-{Jun-29,Jun-3[0-1],Jul}-*  /apps/logs/sct/${YEAR}/July

KISS - keep it simple stupid
leylakhavaninAuthor Commented:
amit,
I have added your comment into my script but when I run it
---------------------------------------------------------------------------------
#!/bin/ksh

YEAR=`date '+%Y'`
MONTH=`date '+%y-%B'`
DAY=`date '+%d'`
BASE_DIR=/apps/logs/sct
PREVIOUS_MONTH=$((`date '+%m'` - 1))

#Check to make sure the Year and Month directories exist if not
#create the current year/month directory
cd ${BASE_DIR}

   if [  -d  ${YEAR} ]
   then
      if [ -d ${YEAR}/${MONTH} ]
      then
        :
      else
       mkdir ${YEAR}/${MONTH}
      fi
   else
     mkdir -p ${YEAR}/${MONTH}
   fi

#Now move the files into the directory.
cp *.---.---.com /apps/logs/sct/${YEAR}/${MONTH}

#Change the directory and create a combined report
cd /apps/logs/sct/${YEAR}/${MONTH}

PREVIOUS_MONTH_PATTERN=`date -d ${YEAR}/${PREVIOUS_MONTH}/${DAY} '+%Y-%b'`
THIS_MONTH_PATTERN=`date '+%Y-%b'`

for i in ${PREVIOUS_MONTH_PATTERN}*.com
do
        if [ `echo $i | cut -d"-" -f3` -ge 28 ]
        then
              echo cp $i /apps/logs/sct/${YEAR}/${MONTH}
        fi
done

for i in ${THIS_MONTH_PATTERN}*.com
do

        if [ `echo $i | cut -d"-" -f3` -lt 28 ]
        then
                echo cp $i /apps/logs/sct/${YEAR}/${MONTH}
        fi
done

rm combined.report
for file in *
do
       pr -f $file >>combined.report
       echo ----------------------------------------------------------------------------->>combined.report
done

This is the error that I get:


date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]


For some reason it does not like -d @
PREVIOUS_MONTH_PATTERN=`date -d ${YEAR}/${PREVIOUS_MONTH}/${DAY} '+%Y-%b'`
Any help on that?
amit_gCommented:
It seems your flavor of UNIX doesn't support date -d option. Is it SUN? I don't know any other method to get previous month's name. May be someone else has a suggestion. Do you have perl installed?
ahoffmannCommented:
date -d is GNU's date only

> PREVIOUS_MONTH_PATTERN=`date ....

does this mean that you try to use a generic way to compute the prvious month?
This is a complicated thing, not to compute, but to define under which conditions what is the previous month.
I'd suggest that we first get this question answered, then you can search for a "previous month" solution (some have been given at EE).
leylakhavaninAuthor Commented:
Back to my initial question:

>Is there any better command (better than cp) to use to be able to have condition check for current month before it copied that file into /${YEAR}/${MONTH}?

>Is there any way to copy the files that are still in month of Jun but after 28? In a simple way, a condition that can check
if the file date is anything after Jun-28 then send them to month of July and if not just leave them in the same directory.
Because some of our cron are set up to run few days before the end of the month.
ahoffmannCommented:
> >Is there any better command (better than cp) ..
AFAIK no on a standard system

> Is there any way to copy the files that are still in month of Jun but after 28? In a simple way ..
see http:#14384982
leylakhavaninAuthor Commented:
ahoffmann,
Thanks for your quick respond. I have already work around that but I need some thing for all 12 months and not only month of June and July or year 2005?!
/apps/logs/sct/2005-Jun-{[1-9],1[0-9],2[0-8]}-Jun* /apps/logs/sct/${YEAR}/June
/apps/logs/sct/2005-{Jun-29,Jun-3[0-1],Jul}-*  /apps/logs/sct/${YEAR}/July
ahoffmannCommented:
and why didn't you then not come up with an answer to http:#14403881

your current definition of "date" is far too vage to give you any hints
Please reread  http:#14403881, then get used yourself to dates

I highly recommend that you rethink what you want to achieve 'cause calculations on calender values make things very very complicated.

A nice starting point to get used to the problems is perl's Date::Manip module and some .txt files in the prostfix sources ;-)
amit_gCommented:
The simplest solution would be to name the files correctly. Either name all June files as Jun, all July as Jul or use number i.e. 06 instead of Jun, 07 instead of Jul. If it it not the month name in the file name, a simple script can be written.
leylakhavaninAuthor Commented:
The format of the file is Jun,Jul and .. as you see below:

2005-Jul-1-nicfs01.---.---.com      
2005-Jul-1-venus.---.---.com
2005-Jul-1-nicfs2.---.---.com      
2005-Jun-30-ecs47m1.---.---.com
2005-Jul-1-nicfs3.---.---.com      
2005-Jun-30-niclinux.---.---.com
2005-Jul-1-nicfs4.---.---.com

ahoffmannCommented:
hmm, my suggestion works for this samples too
amit_gCommented:
I meant to say that files that are supposed to go in July folders should be all named as Jul. As per your posting 29th, and 30th June files are named as 2005-Jun-29... and 2005-Jun-30... but should go to Jul folder. So either name them as some Jul date or move them to June folder.

The other alternative is to use numbers instead of month name i.e. name the 2005-Jun-29... file as 2005-06-29... and 2005-Jun-30 file as 2005-06-30. In this case the numbers can be easily automated. It is the MonthName that is causing the problem.
leylakhavaninAuthor Commented:
I am going to split the point for this question, because some of the requirments on my script have been changed. But thanks for all you help and comments :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.