Home Man
Search
Today's Posts
Register

Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Timestamp format in HP-UX

Tags
advanced, hpux, ls, timestamp

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 06-14-2018
Question 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
Code:
   ls -lF | grep /

OR
Code:
   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.
The Following User Says Thank You to nickz2017 For This Useful Post:
# 2  
Old 06-14-2018
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
The Following User Says Thank You to rbatte1 For This Useful Post:
nickz2017 (06-14-2018)
# 3  
Old 06-14-2018
SysV-derived Unix does not have a stat command. Only the latest Solaris 11 has got it.
I would try perl.
Code:
man perl

leads to
Code:
man perlfunc

for the stat() and localtime() functions.
I pasted the examples, and with a little trial and error I got
Code:
#!/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
Code:
/path/to/perlscript */
find . -type d -exec /path/to/perlscript {} +

The Following 3 Users Say Thank You to MadeInGermany For This Useful Post:
jim mcnamara (06-14-2018), nickz2017 (06-14-2018)
# 4  
Old 06-14-2018
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:

Code:
$ /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.

# 5  
Old 06-14-2018
Code:
# is now
foreach $argfile (@
ARGV) {

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

# 6  
Old 06-14-2018
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.
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Convert date in dd mm yyyy format to UNIX timestamp angshuman Shell Programming and Scripting 1 07-23-2016 09:14 PM
Convert UNIX timestamp to readable format in the file rahul2662 Shell Programming and Scripting 6 09-17-2015 10:29 AM
Getting file timestamp in certain format Braun Shell Programming and Scripting 4 07-13-2012 04:31 PM
Format/Fix Timestamp Data in a File. mtlrsk UNIX for Dummies Questions & Answers 3 01-08-2011 09:50 AM
Convert unix timestamp to year month day format ? vilius AIX 1 01-06-2010 02:30 AM
To convert a date(in string format) to unix timestamp girish.raos Shell Programming and Scripting 11 09-25-2009 10:33 AM
ls -l timestamp display format pok.fung UNIX for Advanced & Expert Users 3 08-11-2009 02:22 PM
converting unix timestamp into readable format using c++ uselessprog Programming 3 04-07-2008 03:07 PM
get the timestamp of a file in desired format sumeet UNIX for Advanced & Expert Users 1 03-08-2007 11:24 AM
how to convert from timestamp to date format in tcsh umen Shell Programming and Scripting 2 11-22-2005 03:51 AM


All times are GMT -4. The time now is 12:47 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password