9 find-by-date - Find files with GNU find(1) but with easier to comprehend time interval formats
13 find-by-date [I<FROM>--][I<TO>] [I<FIND-ARGS>]
17 Takes your I<FROM>--I<TO> date-time specifications and turns into the
18 appropriative C<< -mmin -I<MINUTES> >> and C<< -mmin +I<MINUTES> >>
19 parameters for find(1), then call find(1).
21 =head1 SUPPORTED DATE FORMATS
23 Recognize these date-time formats in I<FROM> and I<TO>:
39 Enter C<< 0--I<TO> >> to select any time up to I<TO>.
40 Enter C<< I<FROM>-- >> to select any time starting from I<FROM>.
50 trap 'echo "Internal error in ${FUNCNAME:-main}" >&2; exit -2' ERR
57 current_dt
=`date +%Y%m%d%H%M -d @$current_ts`
58 current_Year
=${current_dt:0:4}
59 current_month
=${current_dt:4:2}
60 current_day
=${current_dt:6:2}
61 current_Hour
=${current_dt:8:2}
62 current_Min
=${current_dt:10:2}
64 timeunits
=(Year month day Hour Min
)
68 formats
+=("YYYY-mm-dd_HH:MM")
69 fmt['([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})_([0-9]{1,2})[.:]([0-9]{1,2})']="Year month day Hour Min"
70 formats
+=("YYYY-mm-dd_HH")
71 fmt['([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})_([0-9]{1,2})']="Year month day Hour"
72 formats
+=("YYYY-mm-dd")
73 fmt['([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})']="Year month day"
75 fmt['([0-9]{4})-([0-9]{1,2})']="Year month"
77 fmt['([0-9]{4})']="Year"
79 fmt['([0-9]{1,2})-([0-9]{1,2})']="month day"
81 fmt['([0-9]{1,2})']="day"
82 formats
+=(" mm-dd_HH:MM")
83 fmt['([0-9]{1,2})-([0-9]{1,2})_([0-9]{1,2})[.:]([0-9]{1,2})']="month day Hour Min"
84 formats
+=(" mm-dd_HH")
85 fmt['([0-9]{1,2})-([0-9]{1,2})_([0-9]{1,2})']="month day Hour"
86 formats
+=(" dd_HH:MM")
87 fmt['([0-9]{1,2})_([0-9]{1,2})[.:]([0-9]{1,2})']="day Hour Min"
89 fmt['([0-9]{1,2})_([0-9]{1,2})']="day Hour"
91 fmt['([0-9]{1,2})[:.]']="Hour"
93 fmt['_([0-9]{1,2})']="Hour"
98 echo "Usage: $0 [from-date][\"--\"till-date] [find-parameters]
100 for f
in "${formats[@]}"
110 ts
=`date +%s -d "$1"`
111 echo $
[ ($current_ts - $ts) / 60 ]
121 for timeunit
in "${timeunits[@]}"
123 eval "${boundary}_$timeunit="
126 for regex
in "${!fmt[@]}"
128 if [[ "${!boundary}" =~ ^
$regex$
]]
131 for timeunit
in ${fmt[$regex]}
133 eval ${boundary}_
$timeunit=${BASH_REMATCH[$n]}
139 [ -n "$n" ] && return 0 ||
return 1
156 day
) date +%d
-d "$3-$4-1 + 1 month - 1 day" ;;
173 local longer_timeunit longer_var longer_val
174 local shorter_timeunit shorter_var shorter_val
176 local current_var current_val
177 local current_shorter_var current_shorter_val
179 while [ $miss -gt 0 ]
182 for n
in "${!timeunits[@]}"
184 timeunit
=${timeunits[$n]}
185 var
=${boundary}_
$timeunit
193 longer_timeunit
=${timeunits[$[n-1]]}
194 longer_var
=${boundary}_
$longer_timeunit
195 longer_val
=${!longer_var}
198 if [ -n "$longer_val" ]
200 var_Y
=${boundary}_Year
201 var_m
=${boundary}_month
202 val
=`timeunit_limit $boundary $timeunit ${!var_Y} ${!var_m}`
205 if [ $
[n
+1] -ge ${#timeunits[@]} ]
209 shorter_timeunit
=${timeunits[$[n+1]]}
210 shorter_var
=${boundary}_
$shorter_timeunit
211 shorter_val
=${!shorter_var}
214 if [ -n "$shorter_val" ]
216 if [ "$boundary" = till
]
218 current_var
=from_
$timeunit
219 current_shorter_var
=from_
$shorter_timeunit
221 ts
=`date +%s -d "$from_Year-$from_month-$from_day $from_Hour:$from_Min"`
224 current_var
=current_
$timeunit
225 current_shorter_var
=current_
$shorter_timeunit
230 current_val
=${!current_var}
231 current_shorter_val
=${!current_shorter_var}
233 if [ "$shorter_val" $cmp "$current_shorter_val" ]
235 eval $var=$current_val
237 dt
=`date +%Y-%m-%dT%H:%M -d @$ts`
238 val
=`date +%${timeunit:0:1} -d "$dt $op 1 $timeunit"`
259 if [ "$arg" = --help ]
268 if [[ "$1" =~ ^
(.
*?
)--(.
*)$
]]
270 from
=${BASH_REMATCH[1]}
271 till
=${BASH_REMATCH[2]}
272 elif [[ "$1" =~ ^
(.
*)--$
]]
274 from
=${BASH_REMATCH[1]}
275 elif [[ "$1" =~ ^
0--(.
*)$
]]
277 till
=${BASH_REMATCH[1]}
285 # TODO support more intervals
287 for boundary
in from till
289 if [ -n "${!boundary}" ]
291 parsedatetime
$boundary
292 complete_times
$boundary
301 printf "From: %04d-%02d-%02d %02d:%02d\n" $from_Year $from_month $from_day $from_Hour $from_Min >&2
302 mins
=`ago_min "$from_Year-$from_month-$from_day $from_Hour:$from_Min"`
303 find_opts
+=(-mmin -$mins)
307 printf "Till: %04d-%02d-%02d %02d:%02d\n" $till_Year $till_month $till_day $till_Hour $till_Min >&2
308 mins
=`ago_min "$till_Year-$till_month-$till_day $till_Hour:$till_Min"`
309 find_opts
+=(-mmin +$mins)
312 find "${find_opts[@]}" "$@"