Slightly OT, .. selecting files by mtime as YYMM

2 views
Skip to first unread message

L V Lammert (Omnitec Corp)

unread,
May 7, 2013, 10:52:22 AM5/7/13
to vim-...@googlegroups.com
This group has some of the best bash folks I know, and it seemed like a good place to post the question:

I need to select files in a directory based on a "YYMM" criteria - i.e. YYMM01 < mtime < YYMM31 [or month end].

I can envision the expansion of YYMM to YYMM01, but i am at a loss for a way to handle the variable YYMM<end>.

Any thoughts would be appreciated!

    Thanks!

    Lee

Bob Ernst

unread,
May 21, 2013, 8:33:56 PM5/21/13
to vim-...@googlegroups.com
Leaning heavily on the date command:
    date --date="130501 next month yesterday" +%y%m%d
gives the result
    130531



--
You received this message because you are subscribed to the Google Groups "Vim Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim-geeks+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Don Ellis

unread,
May 22, 2013, 9:29:24 PM5/22/13
to vim-geeks
Looks like that's a GNU version of date, not the BSD version, which gives:

1 (501) $  date --date="130501 next month yesterday" +%y%m%d
date: illegal option -- -
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

I'll be looking at other sources for the date command; brew doesn't list it under that name. Found find under findutils. [Later: found it in coreutils package - will brew install that and try it again.]

--Don Ellis

Bob Ernst

unread,
May 23, 2013, 12:34:25 AM5/23/13
to vim-...@googlegroups.com
Yes, I was using the GNU date command. Here is a more painful bash alternative starting with a four-digit year and the month.

#!/bin/bash
YEAR=2013
MONTH=5
days=(0 31 28 31 30 31 30 31 31 30 31 30 31)
lastday=${days[$MONTH]}

if [ $MONTH -eq 2 ]; then
    if [ $((YEAR % 4)) -ne 0 ] ; then
        : # still 28
    elif [ $((YEAR % 400)) -eq 0 ] ; then
        lastday=29
    elif [ $((YEAR % 100)) -eq 0 ] ; then
        : # still 28
    else
        lastday=29
    fi
fi
echo "The last day of the month is $YEAR-$MONTH-$lastday"

Nathan Neff

unread,
May 23, 2013, 12:50:30 PM5/23/13
to vim-...@googlegroups.com
I would just get the year month from YYMM and then find files whose
mtime had that month, and not worry about the 29 vs. 30 vs. 31

the "stat" command will give you the mtime
stat -c '%y' some-file
2013-05-21 23:23:10.529603934 -0400

If I needed 1305, I would stat every file like above and only select lines/files
that started with 2013-05


Reply all
Reply to author
Forward
0 new messages