Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Senario: 5 Directories, each having year/month wise logs, had to archive in yearly and further monthly basis, easiest way to have would be writing script. so here we go…

#!/bin/bash
LOCATION=`pwd`
MM=`date +%m --date='1 month ago'`
if [ "$MM" = "12" ]; then
                 echo "It was previous Year"
                 YYYY=`date +%Y --date='1 year ago'`
                 echo "Now YYYY = $YYYY"
                 else
                 YYYY=`date +%Y`
fi

for file in $LOCATION;do
                 file=`ls -d */*-$YYYY-$MM`
                 echo "What is the value of file: $file"
                 for i in $file; do
                 echo "what is the value of i: $i"
                folder=`echo $i | cut -d'/' -f1`
                echo "file yesto huncha $folder"
                Final_Location=$LOCATION/$i
                echo "what is the value of Final_Location: $Final_Location"


echo " Directory cha ki nai bhanera herne: $Final_Location"
if [ -d "$Final_Location" ]; then
                echo "File doesnot exist"

else
                                        echo "Move files chop chop"
        fi
                                if [ -d "$LOCATION/$folder/$YYYY" ]; then
                                        echo "YEAR cahiyena No Need to worry"
                                 else

                                        echo "Creating DIR: mkdir -p $LOCATION/$folder/$YYYY"
                                        `mkdir -p $LOCATION/$folder/$YYYY`
                                 fi


                                 if [ -d "$LOCATION/$folder/$YYYY/$MM" ]; then
                                        echo "Month Cahiyena No Need to worry"
                                 else

                                        echo "Creating DIR: mkdir -p $LOCATION/$folder/$YYYY/$MM"
                                        `mkdir -p $LOCATION/$folder/$YYYY/$MM`
                                 fi

                                echo "Moving File"
                                echo "mv $Final_Location $LOCATION/$folder/$YYYY/$MM"
                                `mv $Final_Location $LOCATION/$folder/$YYYY/$MM`


done
done