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:
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.
Is there any other way to display this information without using C or Perl, by just using standard commands and parameters?
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! :)
sh dir_list.sh
but it doesn't provide the output in the target directory (says directory path^M: not found.
). It shows the output only from the directory from where I am executing the script, in this case /dev/scripts. Is there a way to specify output only in the target directory mentioned in shell script?