3

Consider dates in the ISO 8601 format i.e. ‘YYYY-MM-DD’, e.g. 2015-01-28, and say we have a folder with files of the form:

AAAA_<date>_BBBB

I am looking for a glob pattern in zsh (or Bash) that I could use to specify files between two dates.

For example, say I want to copy files between 2014-12-15 and 2015-02-03. Is there an easy way to build a glob expression to refer to files between those two dates (including them)?

The following may help (from lexicographical order):

An important exploitation of lexicographical ordering is expressed in the ISO 8601 date formatting scheme, which expresses a date as YYYY-MM-DD. This date ordering lends itself to straightforward computerized sorting of dates such that the sorting algorithm does not need to treat the numeric parts of the date string any differently from a string of non-numeric characters, and the dates will be sorted into chronological order.

  • No, that would even be really ugly and verbose with full-featured regex. Just loop and compare. – jordanm Feb 4 '15 at 2:20
  • @jordanm I have udpated the question in case it helps. – Amelio Vazquez-Reina Feb 4 '15 at 2:29
3

It is good you tagged the question with zsh, since in that shell you can use extensive glob qualifiers including test done on file names:

$ ls -1 AAAA*
AAAA_2012-10-03_BBBB
AAAA_2014-12-28_BBBB
AAAA_2015-01-03_BBBB
AAAA_2015-02-03_BBBB
AAAA_2015-10-03_BBBB

$ d1='2014-12-15'
$ d2='2015-02-03'
$ print -l *(e:'[[ "${${REPLY#*_}%_*}" > "$d1" && "${${REPLY#*_}%_*}" < "$d2" ]]':)
AAAA_2014-12-28_BBBB
AAAA_2015-01-03_BBBB

Note that for the given date format you can perform simple string comparison.

I assumed here, that your files are named AAAA_date_BBBB as in the question and extracted the "date" part with parameter expansion. You would probably need to modify this code to get "date" in suitable way for your real case scenario.

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.