Timestamp format in HP-UX

I am on HP-UX B.11.11 OS. My requirement is to display a list of directories (not files) and the last modified time format should be DD-MON-YYYY HH:MI:SS AM/PM. I am able to get the list using either

   ls -lF | grep /

OR

   ls -ld -- */
 but I am unable to set the time format as I want. The --full-time parameter doesn't work in HP-UX, and HP-UX doesn't have stat as well.

Questions:

  1. I can try with Perl (located at /usr/bin/perl) but I am not very well versed with it. Could anyone please provide me a script to get the same output as that of ls -ld */

  2. Is there any other way to display this information without using C or Perl, by just using standard commands and parameters?

  3. I was wondering how is WinSCP able to display the full date/time format in the UI ? Anyone knows what command it uses internally to display the directory contents in the UI?

Any help is appreciated.

You would be best looking at the stat command to get the information you want in the format you want. The output of ls is slightly fickle. If a file is older than 6 months it may display a day/month/year rather than a day.month/time which might confused your process.

Using stat and the appropriate flags, you can build the time in a suitably portable way.

Have a go and let us know if you get stuck and we will try to help you.

Kind regards,
Robin

1 Like

SysV-derived Unix does not have a stat command. Only the latest Solaris 11 has got it.
I would try perl.

man perl

leads to

man perlfunc

for the stat() and localtime() functions.
I pasted the examples, and with a little trial and error I got

#!/usr/bin/perl
foreach $argfile (@ARGV) {
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        $atime,$mtime,$ctime,$blksize,$blocks) = stat($argfile);
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
    printf "%s-%02d-%02d %02d:%02d:%02d %s\n", 
        $year+1900, $mon+1, $mday, $hour, $min, $sec, $argfile;
}

Now you can make it executable and run it with

/path/to/perlscript */
find . -type d -exec /path/to/perlscript {} +
2 Likes

Thanks for looking into it. I executed the script you provided but ended up with some errors. It might be a formatting error from my side, since I am pretty new to Unix/Perl. This is what I got:

$ /usr/bin/perl
foreach $argfile (@
ARGV) {
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        $atime,$mtime,$ctime,$blksize,$blocks) = stat($argfile);
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
    printf "%s-%02d-%02d %02d:%02d:%02d %s\n",
        $year+1900, $mon+1, $mday, $hour, $min, $sec, $argfile;
}
syntax error at - line 1, near "ARGV) "
syntax error at - line 7, near "}"
Execution of - aborted due to compilation errors.
# is now
foreach $argfile (@
ARGV) {

# should be
foreach $argfile (@ARGV) {
# no line feeds

Thanks @rbatte1 and @MadeInGermany

I posted the question in Unix StackExchange as well, and found the solution. I have posted the final script there in case it's helpful for others with same issue/question.

I am unable to paste the web link to the StackExchange thread here since I guess I have to have at least 5 posts, which I don't. I wish I was able to provide the link so that it would have been helpful to others stumbling on the post in future.

---------- Post updated at 06:24 PM ---------- Previous update was at 06:23 PM ----------

Thanks @rbatte1 for your help as well, but unfortunately even stat is not available in HP-UX version we have.