The GNU implementation of the date command has a nice feature to show the date of the last modification of a reference file. Then using the +FORMAT parameter it's easy to get the date in any format, for example:

date -r /etc/motd +%Y%m%d_%H%M%S
# output in the format: 20121001_171233

Is there an equivalent for this in Solaris? As a workaround, I do it using the stat function of perl like this:

perl -mPOSIX -e 'print POSIX::strftime("%Y%m%d_%H%M%S\n", localtime((stat("/etc/motd"))[9]))'

Another alternative is to parse the output of ls -Ego:

ls -Ego /etc/motd | awk '{print $4 "_" $5}' | tr -d :- | sed -e 's/\..*//'

But parsing the output of ls is known to be a bad practice.

Is there a better/simpler/more elegant solution?

3 Answers 3


The inability of date to print anything but the current date is an oft-lamented defect. Some unices allow it to print other dates, but sadly for you not Solaris.

As long as you only need numerical output, you can parse the output of ls -Ego. The options -go suppres the user and group name, and -E is a Solaris-specific option to ls that makes it display the time in a parseable format.

ls -Ego /path/to/file |
awk 'NR==1 {$0 = $4 "_" $5; gsub(/[-:]/,""); sub(/\..*/, ""); print}'

(This works with /usr/bin/ls but requires a POSIX-compliant awk, so make sure to have /usr/xpg4/bin or /usr/xpg6/bin ahead of /usr/bin on your PATH, or call nawk.)


I think that Perl is your best bet; I don't know of any useful date converter in Solaris, and date command only prints or sets current time.


Well not exactly the format you want but how about

stat -c %y /etc/motd
  2011-03-08 11:39:20.000000000 +0000

and then use whatever you prefer to bring that timestamp into shape.

I'm assuming OpenSolaris or Solaris 11 where the GNU version of stat is bundled.

  • Is there a stat command in Solaris? Not that I know of. (* with exception of the ksh shell, where there is a shell builtin, I'm told)
    – January
    Commented Oct 11, 2012 at 20:20
  • There is a stat command in Solaris 11, but then if you have Solaris 11, there is also the simpler solution of just using /usr/gnu/bin/date.
    – alanc
    Commented Oct 12, 2012 at 3:10
  • I'm in Solaris 10. I found GNU date in /usr/local/bin/date. That's a good workaround, but it would be good to know another solution in case GNU date is not available.
    – janos
    Commented Oct 12, 2012 at 8:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .