Get List of Directories and Timestamp in a specific format in HP-UX

hp-uxlstimestamps

I am on HP-UX B.11.11 OS. My requirement is to display a list of directories only 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 or –time-style parameter doesn't work in HP-UX, and HP-UX doesn't have stat as well.

Questions:

  1. Primary Requirement: Could anyone please provide me a script to display the list of all the directory names (not files) under current directory and the last modified timestamp in the format mentioned above? I don't need the owner name, group name, size, permissions etc.

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

UPDATE (edits below only):
So, Stéphane Chazelas's answer with perl script worked perfectly. Now, I am trying to convert it into a shell script, but I am getting errors while executing it. I have saved the shell script dir_list.sh under /dev/scripts/. Could you please help where I am going wrong?

#!/usr/bin/sh
# dir_list.sh : Generate a comma separated directory list with last modified timestamp
# Navigate to the directory where listing is required
cd /dev/product/jobs
# Execute Perl script
/usr/bin/perl -MPOSIX -MFcntl -MFile::stat -le '
  setlocale(LC_TIME, "C");
  for (<*>) {
    $s = lstat $_ or die "$_: $!\n";
    print "$_," . uc(strftime("%d-%b-%Y %I:%M:%S %p", localtime $s->mtime))
      if S_ISDIR($s->mode)
  }'
exit 0

ERROR MESSAGE
Please note that I tried #!/usr/bin/sh as well, but it failed with same error message: interpreter "/usr/bin/sh" not found

$ ./dir_list.sh
interpreter "/bin/sh" not found
file link resolves to "/usr/bin/sh"
ksh: ./dir_list.sh:  not found

Final Update : RESOLVED – Solution Below

I created a Unix shell script dir_list.sh which when called ( $ ./dir_list.sh ) searches within the target folder specified in the script and displays the folder names along with its associated timestamp as a comma-separated records

#! /usr/bin/ksh
# dir_list.sh : Generate a comma separated directory list with last modified timestamp
#
# Navigate to the Target Directory
cd /dev/product/jobs || exit
#
# Execute Perl script to format the output
/usr/bin/perl -MPOSIX -MFcntl -MFile::stat -le '
  setlocale(LC_TIME, "C");
  for (<*>) {
    $s = lstat $_ or die "$_: $!\n";
    print "$_," . uc(strftime("%d-%b-%Y %I:%M:%S %p", localtime $s->mtime))
      if S_ISDIR($s->mode)
  }'
#
exit 0

Thanks to Stéphane Chazelas for all your help!
🙂

Best Answer

Unless GNU utilities are installed, your best bet is probably perl on those traditional systems:

perl -MPOSIX -MFcntl -MFile::stat -le '
  setlocale(LC_TIME, "C");
  for (<*>) {
    $s = lstat $_ or die "$_: $!\n";
    print "$_ " . uc(strftime("%d-%b-%Y %I:%M:%S %p", localtime $s->mtime))
      if S_ISDIR($s->mode)
  }'

That's perl's interface to the standard POSIX lstat() system call that retrieves file metadata and strftime() function to format dates.

See perldoc POSIX, perldoc -f lstat, perldoc -f stat, man lstat, man strftime for details. We use the C locale for LC_TIME so we get English month names and PM/AM regardless of the preferences of the user.

If zsh is installed:

zsh -c 'zmodload zsh/stat
        LC_ALL=C stat -nA times -LF "%d-%b-%Y %I:%M:%S %p" +mtime -- *(/) &&
          for f t ($times) printf "%s\n" "$f: ${(U)t}"'

Above, we're using perl's uc() and zsh's ${(U)var} to convert the timestamps to uppercase. On GNU systems, you could have used %^b for a all-uppercase month abbreviation, but it doesn't look like it's available on HP/UX.

Related Question