Date Compare Script

Hi All,

I wil be having Files as below in SourceFile_Path DIR

AA.20110131
AA.20110228
AA.20110202
AA.20110330
BB.20091031

I want to Keep only monthly (20110131,20110228,20100229,20110330) in First SourceFile_Path, If it is not monthly file then want to Move files to some another DIR(Target_DIR)
I need to write logic in shell Script.
Please Suggest.

SourceFile_Path=${MDIR}/MG_SrcFiles
Segment_List="Seg_List.lst"
date_List="Date_Files.lst"
Target_DIR==${TDIR}/TG_SrcFiles
#Seg_List.lst Contained
#AA
#BB

while read filelist
do
  if test -s ${SourceFile_Path}/${date_List}
  then
    ls -r -1 AA.20110330| cut -d '.' -f2>>${SourceFile_Path}/${date_List}
  else
    ls -r -1 AA.20110330| cut -d '.' -f2>${SourceFile_Path}/${date_List}
  fi
done<${Segmentlist_Path}/${Segment_List}

So you decide which month to keep and move the files to the appropriate directory?

Hi,

I want to Keep only monthly files in FIRST DIR..
If Files are of other date, I need to move that files to another DIR.

Please Suggest me Solution.

Thanks,
Sam

Got it

---------- Post updated at 06:45 AM ---------- Previous update was at 06:28 AM ----------

Have a look at this, I just put them in a long string separating them by '-' and then I use awk to get you the file names matching your date.

echo "AA.20110131-AA.20110228-AA.20110202-AA.20110330-BB.20091031" | awk ' BEGIN{RS="-"} /201102/ {print}'

---------- Post updated at 06:49 AM ---------- Previous update was at 06:45 AM ----------

You can do something like this:

ls -lrt *AA* | awk ' /201102/ {print $NF}'

Having the files

AA.20110131
AA.20110202
AA.20110228
AA.20110330
BB.20091031

you will get

AA.20110228
AA.20110202

Thanks a lot for Reply..

But in
ls -lrt *AA* | awk ' /201102/ {print $NF}'
201102 I can't Hardcode it. It should be dynamic.

After some time AA.20061111 files wil come, In that case need this also..

Thanks,
Sam

If you want to keep files, you can use the following logic:

# In this case it will look for directories from 30 until 50 (30 + 20) days ago!
countValBack=20
initVal=30
countValues=`expr ${initVal} + ${countValBack}`

basePath="./"

while [ ${countValues} -ge ${initVal} ]
do
	currDate=`date -d "${countValues} days ago" +"%Y%m%d"`
	bpDate="${basePath}/${currDate}"
	if [ -d "${bpDate}" ]
	then
		echo "Removing directory: [${bpDate}] - [${countValues}] days ago..."
		# rm -rf "${bpDate}"
		if [ ${?} -eq 0 ]
		then
			echo "Removed successffully!"
		else
			echo "Failed to remove."
		fi
	fi
	
	countValues=`expr ${countValues} - 1`
done

This in in this post!

A few changes will suit your needs!

I hope it helps.