Timestamps for Log Files

There are two common occasions when you might want to get a timestamp

  • If you want to create a logfile called “myapp_log.11.Mar.2007”
  • If you want to write to a logfile with “myapp: 11 Mar 2007 22:14:44: Something Happened”

Either way, you want to get the current date, in the format you prefer – for example, it’s easier if a filename doesn’t include spaces.

For the purposes of this article, though for no particular reason, I am assuming that the current time is 10:14:44 PM on Sunday the 11th March 2007.

The tool to use is, naturally enough, called “date“. It has a bucket-load of switches, but first, we’ll deal with how to use them. For the full list, see the man page (“man date“), though I’ll cover some of the more generally useful ones below.

Setting the Date/Time

The first thing to note, is that date has two aspects: It can set the system clock:

# date 031122142007.44

will set the clock to 03 11 22 14 2007 44 – that is, 03=March, 11=11th day, 22 = 10pm, 14 = 14 minutes past the hour, 2007 = year 2007, 44 = 44 seconds past the minute.

Heck, I don’t even know why I bothered to spell it out, it’s obvious. Of course the year should come between the minutes and the seconds (ahem).

Getting the Date/Time

The more often used feature of the date command, is to find the current system date / time, and that is what we shall focus on here. It doesn’t follow tradition, in that it uses the “+” and “%” symbols, instead of the “-” symbol, for its switches.

H = Hours, M = Minutes, S = Seconds, so:

$ date +%H:%M:%S
22:14:44

Which means that you can name a logfile like this:

#!/bin/sh
LOGFILE=/tmp/log_`date +%H%M%S`.log
echo Starting work > $LOGFILE
do_stuff >> $LOGFILE
do_more_stuff >> $LOGFILE
echo Finished >> $LOGFILE

This will create a logfile called /tmp/log_221444.log

You can also put useful information to the logfile:

#!/bin/sh
LOGFILE=/tmp/log_`date +%H%M%S`.log
echo `date +%H:%M:%S : Starting work > $LOGFILE
do_stuff >> $LOGFILE
echo "`date +%H:%M:%S : Done do_stuff" >> $LOGFILE
do_more_stuff >> $LOGFILE
echo "`date +%H:%M:%S : Done do_more_stuff" >> $LOGFILE
echo Finished >> $LOGFILE

This will produce a logfile along the lines of:

$ cat /tmp/log_221444.log
22:14:44: Starting work
do_stuff : Doing stuff, takes a short while
22:14:53: Done do_stuff
do_more_stuff : Doing more stuff, this is quite time consuming.
22:18:35: Done do_more_stuff
$

Counting the Seconds

UNIX has 1st Jan 1970 as a “special” date, the start of the system clock; GNU date will tell you how many seconds have elapsed since midnight on 1st Jan 1970:

$ date +%s
1173651284

Whilst this information is not very useful in itself, it may be useful to know how many seconds have elapsed between two events:

$ cat list.sh
#!/bin/sh
start=`date +%s`
ls -R $1 > /dev/null 2>&1
end=`date +%s`

diff=`expr $end - $start`
echo "Started at $start : Ended at $end"
echo "Elapsed time = $diff seconds"
$ ./list.sh /usr/share
Started at 1173651284 : Ended at 1173651290
Elapsed time = 6 seconds
$

For more useful switches, see the man page, but here are a few handy ones:

$ date "+%a %b %d" # (in the local language)
Sun Mar 11
$ date +%D         # (show the full date)
03/11/07
$ date +%F         # (In another format)
2007-03-11
$ date +%j         # (how many days into the year)
070
$ date +%u         # (day of the week)
7
$

8 Responses to Timestamps for Log Files

  1. […] post by unixshell and software by Elliott Back […]

  2. HappyD says:

    Great tutorial, now I have a special twist and question for you. I am on an AIX host which for whatever reason IBM decided not to use GNU date. I have two existing dates already pulled from a log file and I would like to get the time between them. Generally I would use the same method you do only like this: $date -d “Fri Mar 16 12:06:03 MST 2007” +%s, but an input option does not exist on AIX’s date command to use another date besides sysdate 😦 Is there any other way that you know of to take an existing date string and convert it into seconds from the unix epoch? I’ve thought about doing it in temp tables in Oracle, but that’s an overly taxing way to handle what should be a quick timestamp calculation…

  3. unixshell says:

    It should be simple, but it’s not. I made the mistake a few years ago, of writing a shell script which used GNU’s ‘%s’ switch… I didn’t think to test it on Solaris, but the person I’d written the script for was running Solaris, which has same (UNIX) syntax as AIX. The workaround I used was to write a very small C program, like this:

    /* start of C program */
    #include <stdio.h>
    #include <time.h>

    int main()
    {
    time_t t;
    (void)time(&t);
    printf(“%d\n”, t);
    }
    /* end of C program */

    This provides the same output as `date +%s`:

    $ ./d ; date +%s
    1174077604
    1174077604
    $

  4. […] Timestamps for Log Files « *nix Shell […]

  5. […] Timestamps for Log Files March 20074 comments 3 […]

  6. […] shell date string from: [1] […]

  7. shravee says:

    I would like to have the alternative of date -d in AIX so that I can get the dates for the exact days i require from a log file.

    do we have any such alternative.

Leave a reply to unixshell Cancel reply