Posts

Showing posts from May, 2013

Linux date usage

Useful date functions In bash script, date is really useful function. Below are some frequently used situations: 1. generate today's datetime: In terminal:~$ date +%Y-%m-%dT%T result: 2013-05-15T16:17:35 In bin/bash: date +"%Y-%m-%d %T" result: 2013-05-15 16:17:35 NOTE: the usage of space. In the terminal, space or "" is not allowed. 2.generate date/datatime n days before: In terminal: ~$date +%Y%m%d -d "60 days ago"  or  ~$date +%Y%m%d -d "60 day ago" result: 20130316 In bin/bash: date +%Y%m%d -d "187 days ago" (same as terminal) 3. generate date in cron job * * * * * test.sh `date +\%Y\%m\%d -d "1 days ago"` 4. Use variable in date enddate=YYYYmmdd test_date=$(date -d"${enddate} - 1 day" + %Y-%m-%d ) note: even need the same format as $enddate, the format definition in the red part still needs to be given.