The date Command

Last Updated on 10/01/2009 by dboth

The date program is usually used to display or set the system date however it can also be used to perform other date related functions in shell scripts.

One example of this is the ability to perform simple date calculations. I currently use this functionality in scripts to calculate dates for creating filenames or directories. This capability is not evident from the man or info pages.

The following date formats are fairly simple examples.

[dboth@dev1 trunk]$ date -d ‘now-15days’ ‘+%Y-%m-%d’
[dboth@dev1 trunk]$ date -d ‘now-16days’ ‘+%Y-%m-%d’
[dboth@dev1 trunk]$ date -d ‘yesterday-16days’ ‘+%Y-%m-%d’

For details on the syntax of the date command, please refer to the man or info pages.

Leave a Reply