KrzyH
KrzyH

Reputation: 4326

Finding files within date range suffix

I have list of log files which names has date suffix:

mylog.2014-01-01.gz
mylog.2014-01-02.gz
........
mylog.2014-03-04.gz

I have a start and end date, for example 2014-02-01 -> 2014-02-04. Using bash on linux script I want to find files with name matching date range. So I want to get files:

mylog.2014-02-01
mylog.2014-02-02
mylog.2014-02-03
mylog.2014-02-04

I can not depend on

find /path -type f -newer $startFile -not -newer $endFile mylog*gz

because mylog.2014-02-01 can be modified on 2014-02-01 23:59 or 2014-02-02 00:01

Upvotes: 3

Views: 124

Answers (1)

PlasmaPower
PlasmaPower

Reputation: 1878

Maybe:

START_DATE=$(date -d '2014-02-01 01' '+%s')
END_DATE=$(date -d '2014-02-04 01' '+%s')
IFS=.
for i in mylog.*.gz; do
    fname=($i)
    d=$(date -d "${fname[1]} 01" '+%s')
    if (($d > $START_DATE && $d < $END_DATE)); then
        echo "Found file: $i"
    fi
done

Thanks to @kojiro for the IFS and glob ideas.

Upvotes: 1

Related Questions