Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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.)

share|improve this answer
    
Nice, thanks! In your answer you are mentioning both -e and -E, that's a bit confusing. In the Solaris I have here (5.10) it works only with -E, and using nawk instead of awk. Actually this way is easier for me: ls -Ego /path/to/file | awk '{print $4 "_" $5}' | tr -d : | sed -e 's/\..*//' –  janos Oct 15 '12 at 7:25
    
@janos I meant -E throughout. What's wrong with the awk code? I didn't think I'd used any feature that isn't supported by Solaris's /usr/bin/awk. –  Gilles Oct 15 '12 at 8:55
    
the awk in this system here is /usr/bin/awk, but it doesn't have neither sub nor gsub and it also cannot assign values to $0. Definitely the most primitive awk I've ever seen. Bottom of man page: SunOS 5.10 Last change: 4 May 2010 –  janos Oct 15 '12 at 9:09

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.

share|improve this answer

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.

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

share|improve this answer
    
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 Oct 11 '12 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 Oct 12 '12 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 Oct 12 '12 at 8:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.