By default, date tells you the current date and time:
% date Mon Jan 6 15:44:21 PST 1997
Fortunately there are options to manipulate the format. The format option is preceded by a + followed by any number of field descriptors indicated by a % followed by a character to indicate which field is desired. The allowed field descriptors are:
%n | => | a newline |
%t | => | a tab |
%m | => | month of year (01-12) |
%d | => | day of month (01-31) |
%y | => | last two digits of year (00-99) |
%D | => | date as mm/dd/yy |
%H | => | hour (00-23) |
%M | => | minute (00-59) |
%S | => | second (00-59) |
%T | => | time as HH:MM:SS |
%j | => | day of year (001-366) |
%w | => | day of week (0-6) Sunday is 0 |
%a | => | abbreviated weekday (Sun-Sat) |
%h | => | abbreviated month (Jan-Dec) |
%r | => | 12-hour time w/ AM/PM (e.g., "03:59:42 PM") |
Note that this is all in the date(1) man page, so don't try to memorize it or anything.
So you can almost duplicate the default with a format of "+%a %h %d %T %y":
% date '+%a %h %d %T %y' ; date Mon Jan 06 16:07:23 97 Mon Jan 6 16:07:23 PST 1997
Anyway, my favorite application of the format is to generate filenames which always come out in date order when sorted alphabetically in ls(1) output.
% date '+tran.%y%m%d.%H%M%S' tran.970106.162446
% clearmake |& tee `date '+tran.%y%m%d.%H%M%S'`
% cat tranmake #!/bin/sh tdir=$HOME/bldtrans fname=`/bin/date '+tran.%y%m%d.%H%M%S'` /usr/atria/bin/clearmake $* 2>&1 | /bin/tee $tdir/$fname
There's some other magic there that I'll talk about in a future tip.
For extra credit, what's this one good for: date +%d-%h-%y.%T
Up to the TTTT index
Tuesday Tiny Techie Tips are all © Copyright 1996-1997 by Jeff Youngstrom. Please ask permission before reproducing any of this material.