Tiny Deathstars of Foulness

At a terminal prompt, it is really straight forward to grab the date, simply use the date command, with no arguments and you will get something similar to the following, including the day, date, time (with seconds), time zone and year:

Tue Apr 15 00:40:07 CDT 2009

In a script this can choose fairly challenging, especially in cases where you just need the date stamp without the time and time zone, etc. Here we’re going to grab the current system date from ESX, OS X or Linux (or whatever OSen really) and then use a variable, currentdate, to put that date, formatted into a pretty standard format, YYYYMMDD:

currentdate=`date ”+%c%m%d” | awk ‘{printf $5}’`

Which will output the date as follows:


Now, in our shell script we can create files, add lines to files, etc, with the shortened date stamp. Some of you will be using log analyzers that depend, for example, on unix epoch time. To grab the date formatted as such, the following command can be used:

date -j -f “%a %b %d %T %Z %Y” “`date`” “+%s”

The date command isn’t used as much to set time any more, since most systems rely on a Network Time Protocol (NTP) server to supply date and time information. However, it is worth noting that the date command can also be used to set the time on a computer.

April 15th, 2009

Posted In: Mac OS X, Mac OS X Server, Mass Deployment, Ubuntu, Unix, VMware

Tags: , ,