Hey I feel like I am probably being a little stupid but I cannot seem to find anywhere how to utilize a sequence with symbol in it.

I want to be able to run a sequence from two date and times: 20191110_2330 and 20191111_0200 but I cannot work out how to read over the _. Any advice would be greatly appreciated.

  • You want to include every minute between the two dates? – jesse_b Nov 10 '19 at 22:35
  • Yer it is going to be alot I know but I am using it to find recording files which are time stamped in that manner – Sulli Nov 10 '19 at 22:36
  • 1
    What is the end goal? Can you perhaps use two separate brace expansions 20191110_{2330..2359} 20191111_{0000..0200} – steeldriver Nov 10 '19 at 22:48
  • I am writing a script where the start and finish times are entered in by the user and it will go out to multiple directories and grab the files within that time frame. – Sulli Nov 10 '19 at 22:56
  • 2
    Could it be that you just want to do this: find /some/dir -newermt "2019-11-10-T23:30:00" -not -newermt "2019-11-11-T02:00:00" ? (Replace /some/dir by the dir you are interested in). If needed sed or perl can be used to convert the format. – Garo Nov 10 '19 at 23:15

Possibly overkill but I was able to accomplish this with the following bash function and GNU date(1):

seq_dates () {
    local Y1 M1 D1 d1
    local r
    local m=0
    d1="${Y1}-${M1}-${D1} ${1: -4}"
    until [[ $r == $2 ]]; do
        r=$($date -d "$d1 + $m minutes" '+%Y%m%d_%H%M')
        printf '%s\n' "$r"

You would call it like:

seq_dates 20191110_2330 20191111_0200

It will convert the input start date into a format useable by GNU date and then runs a loop, adding 1 minute to the date each time until it is equal to the given end date.

Note: If the given end date falls before the given start date this will run an infinite loop.


For looping over dates, simply define start and end and loop over all desired time steps.

Just use epoch time for start and end and add 60 seconds or the desired step size at each iteration. Then set output format as desired.

start=$(date -d2019-11-10T23:30 +%s)
end=$(date -d2019-11-11T02:00 +%s)

while [ $ddate -le $end ] ; do
   date -d@"$ddate" +%Y%m%d_%H%M

If you want to convert your date strings to a readable format for the date command, just use sed:

 echo 20191110_2030 | sed 's/\(....\)\(..\)\(..\)_\(..\)\(..\)/\1-\2-\3T\4:\5/'

Unfortunately most date-versions don't support defining the input string format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.