Script to get "last month" (130 Views)
Reply
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 1 of 6 (130 Views)
Accepted Solution

Script to get "last month"

Hi,

I am creating a script that requires me to move files from "last month". So when ls -la the files and grep on `date +%b` (Dec)...what I actually want it to show is not this month but last month. Is there a way to get that value? It would be pretty simple if the dates were in numerical (Dec = 12) but with them in alpha it makes it more difficult...but I figure with you guys, not impossible ;0)

Thanks!
Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,736
Registered: ‎06-21-2000
Message 2 of 6 (130 Views)

Re: Script to get "last month"

Here's a shell script that will do it:

#!/usr/bin/sh

THISMONTH=$(/usr/bin/date +%m)

if (( $THISMONTH == 1 )) ; then
LASTMONTH=12
else
((LASTMONTH=${THISMONTH}-1))
echo "${THISMONTH} ${LASTMONTH}"
fi

case $LASTMONTH in
1|01) LASTMONTHTXT=JAN ;;
2|02) LASTMONTHTXT=FEB ;;
3|03) LASTMONTHTXT=MAR ;;
4|04) LASTMONTHTXT=APR ;;
5|05) LASTMONTHTXT=MAY ;;
6|06) LASTMONTHTXT=JUN ;;
7|07) LASTMONTHTXT=JUL ;;
8|08) LASTMONTHTXT=AUG ;;
9|09) LASTMONTHTXT=SEP ;;
10) LASTMONTHTXT=OCT ;;
11) LASTMONTHTXT=NOV ;;
12) LASTMONTHTXT=DEC ;;
esac

echo "LAST MONTH = ${LASTMONTHTXT}"
Please use plain text.
Honored Contributor
Robert-Jan Goossens_1
Posts: 1,813
Registered: ‎04-04-2000
Message 3 of 6 (130 Views)

Re: Script to get "last month"

Hi Coolmar,

How about using the find command in combination with some reference files?

# touch -m -t 200611010001 /var/tmp/first
# touch -m -t 200611312359 /var/tmp/last

# find . -type f \( -newer /var/tmp/first -a ! -newer /var/tmp/last \) -print

Best regards,
Robert-Jan
Please use plain text.
Honored Contributor
Kent Ostby
Posts: 1,836
Registered: ‎09-26-2000
Message 4 of 6 (130 Views)

Re: Script to get "last month"

Create a shell script:

#!/usr/bin/sh
LASTMONTH=`date +%b | awk -f useit.awk`
ls -la | grep $LASTMONTH

Create a file called useit.awk:

BEGIN{damonth[1]="Jan";damonth[2]="Feb";damonth[3]="Mar";damonth[4]="Apr";
damonth[5]="May";damonth[6]="Jun";damonth[7]="Jul";damonth[8]="Aug";damonth[9]="
Sep";damonth[10]="Oct";damonth[11]="Nov";damonth[12]="Dec";}
{for (idx1 in damonth)
{if (damonth[idx1]==$1)
{if (idx1==1){idx=13}
print damonth[idx1-1];}
}
}

NOTE: In the shell script, you'll need to put the full path of the file "useit.awk."

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Please use plain text.
Honored Contributor
Geoff Wild
Posts: 7,168
Registered: ‎06-04-2001
Message 5 of 6 (130 Views)

Re: Script to get "last month"

ARCDIR=/some/place

# get the current month

M=`date +%b`

for FILE in `ls -al |awk '$6ne"$M"{print $9}'`
do
mv $FILE $ARCDIR
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.
Please use plain text.
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 6 of 6 (130 Views)

Re: Script to get "last month"

Knew I could count on you guys! Thanks :0)
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation