1834137 Members
2388 Online
110064 Solutions
New Discussion

script question

 
SOLVED
Go to solution
Anthony khan
Frequent Advisor

script question

Hi All,

I am running a following script to look for 3 days old file (file has a time stamp with it) and mv it to JAN directory, now what I want is that when the Feb starts, script will create dir by the name FEB and start mv the feb files to FEB dir and so on, any suggestion, Thanks in advance.

filename=" "
cd /log/moe
for filename in `find . \( -name 'MoeL*' -o -name 'MoeP*.log' -o -name 'moe_s*' -o
-name 'moe.log*' \) -mtime +3 -print`
do
mv $filename /archive/logbackup/$filename
done
cd /archive
file=`find /archive/logbackup -type f -print|wc -l`
if [ $file -eq 0 ]
then
exit
else
compress *
mv *.Z /archive/moe/log/JAN/
cd /archive/logbackup
rm *
fi
7 REPLIES 7
Robin Wakefield
Honored Contributor
Solution

Re: script question

Hi Anthony,

Something along these lines:

======================================
month=`date "+%b" | tr [a-z] [A-Z]`
test -d /archive/moe/log/${month} || mkdir -p /archive/moe/log/${month}
filename=" "
cd /log/moe
for filename in `find . \( -name 'MoeL*' -o -name 'MoeP*.log' -o -name 'moe_s*' -o
-name 'moe.log*' \) -mtime +3 -print`
do
mv $filename /archive/logbackup/$filename
done
cd /archive
file=`find /archive/logbackup -type f -print|wc -l`
if [ $file -eq 0 ]
then
exit
else
compress *
mv *.Z /archive/moe/log/${month}/
cd /archive/logbackup
rm *
fi
========================================

I'm assuming on Feb 1st, you don't mind those files from Jan 31st, say, ending up in the FEB directory.

Rgds, Robin.
Darrell Allen
Honored Contributor

Re: script question

Hi,

If you want to use the month as of the time the script runs then do something like this:

.
.
.
compress *
MONTH=`date +%b | tr "[:lower:]" "[:upper:]"`
if [ ! -d /archive/moe/log/${MONTH} ]
then
mkdir /archive/moe/log/${MONTH}
fi
mv *.Z /archive/moe/log/${MONTH}

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Andreas Voss
Honored Contributor

Re: script question

Hi,

what about this:

month=`date +%b|awk '{sub("\.","",$1);print toupper($1)}'`
[ ! -d /archive/moe/log/$month ] && mkdir /archive/moe/log/$month
cd /log/moe
find . \( -name 'MoeL*' -o -name 'MoeP*.log' -o -name 'moe_s*' -o -name 'moe.log*' \) -mtime +3 -print |
while read filename
do
mv $filename /archive/moe/log/$month
compress /archive/moe/log/$month
done

Regards
Andreas Voss
Honored Contributor

Re: script question

Sorry,

Line:
compress /archive/moe/log/$month

should be:
compress /archive/moe/log/$month/$filename

Regards
Anthony khan
Frequent Advisor

Re: script question

Is there any way to avoid sending moe.log.02-01-31 files to FEB directory, I want these files in JAN dir.
Krishna Prasad
Trusted Contributor

Re: script question

Right before the move you can check for month and day.

If it is Feb day 01 mv to Jan
else
mv to Feb.

This is not the exact syntax of course but with all the excellecent examples for using the date command in the other posts. I am sure you get they syntax from those examples.
Positive Results requires Positive Thinking
Darrell Allen
Honored Contributor

Re: script question

Hi again,

You can change your criteria for setting the target directory. Instead of baseing it on the current date you can use the name of the file itself.

Here's a snippet of code to get you going. This assumes the files are named *log.yy-mm-dd and you want to use mm to determine which directory to place the file in.

for i in `ls *log*`
do
month=`echo $i | awk -F. '{print $NF}' | cut -c4-5`
case $month
in
01) MONTH=JAN;;
02) MONTH=FEB;;
.
.
.
12) MONTH=DEC;;
esac
compress $i
if [ ! -d ${MONTH} ]
then
mkdir ${MONTH}
fi
mv $i.Z ${MONTH}
done

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)