- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Script to get "last month"
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 02:06 AM
тАО12-08-2006 02:06 AM
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!
Solved! Go to Solution.
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 02:19 AM
тАО12-08-2006 02:19 AM
Solution#!/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}"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 02:24 AM
тАО12-08-2006 02:24 AM
Re: Script to get "last month"
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 02:36 AM
тАО12-08-2006 02:36 AM
Re: Script to get "last month"
#!/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."
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 02:40 AM
тАО12-08-2006 02:40 AM
Re: Script to get "last month"
# get the current month
M=`date +%b`
for FILE in `ls -al |awk '$6ne"$M"{print $9}'`
do
mv $FILE $ARCDIR
done
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 02:43 AM
тАО12-08-2006 02:43 AM