I am new to linux. How can I print and store date in given date range.
For example I have startdate=2013-03-01 and enddate = 2013-03-25 ; I want to print all date in that range.
Thanks in advance
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-15 enddate=2013-04-14 curr="$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.
echo "$curr" after the test ;). date --date "$curr +1 day" is nicer than the let cur=..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-25 2013-03-01 2013-03-02 2013-03-03 2013-03-04 2013-03-05 2013-03-06 2013-03-07 2013-03-08 2013-03-09 2013-03-10 2013-03-11 2013-03-12 2013-03-13 2013-03-14 2013-03-15 2013-03-16 2013-03-17 2013-03-18 2013-03-19 2013-03-20 2013-03-21 2013-03-22 2013-03-23 2013-03-24 2013-03-25 :read ! dateseq 2018-12-04 2019-12-31. :read appends output to current buffer (file); ! executes the external (BASH) command (dateseq ...).Use date to convert your dates to seconds, do a little maths and convert back:
#/bin/bash dstart=2013-03-01 dend=2013-03-25 # convert in seconds sinch the epoch: start=$(date -d$dstart +%s) end=$(date -d$dend +%s) cur=$start while [ $cur -le $end ]; do # convert seconds to date: date -d@$cur +%Y-%m-%d let cur+=24*60*60 done See man date for more info on date parameters..
one line version:
seq 0 24 | xargs -I {} date +"%Y-%m-%d" -d '20130301 {}day' # this version is ok if the dates not cross next month seq -f'%.f' 20130301 20130325