Print dates in date range linux Print dates in date range linux shell shell

Print dates in date range linux


As long as the dates are in YYYY-MM-DD format, you can compare them lexicographically, and let date do the calendar arithmetic without converting to seconds first:

startdate=2013-03-15enddate=2013-04-14curr="$startdate"while true; do    echo "$curr"    [ "$curr" \< "$enddate" ] || break    curr=$( date +%Y-%m-%d --date "$curr +1 day" )done

With [ ... ], you need to escape the < to avoid confusion with the input redirection operator.

This does have the wart of printing the start date if it is greater than the end date.


An alternate if you want 'recent' dates is:

echo {100..1} | xargs -I{} -d ' ' date --date={}' days ago' +"%Y-%m-%d"

Obviously won't work for arbitrary date ranges.


Another option is to use dateseq from dateutils (http://www.fresse.org/dateutils/#dateseq):

$ dateseq 2013-03-01 2013-03-252013-03-012013-03-022013-03-032013-03-042013-03-052013-03-062013-03-072013-03-082013-03-092013-03-102013-03-112013-03-122013-03-132013-03-142013-03-152013-03-162013-03-172013-03-182013-03-192013-03-202013-03-212013-03-222013-03-232013-03-242013-03-25