Operating System - HP-UX
1748011 Members
3763 Online
108757 Solutions
New Discussion юеВ

Re: List with full timestamp of files

 
Muktha
Frequent Advisor

List with full timestamp of files

Hi,

Using ls -l , we can display the time stamp of file in hours and minutes.
Can we display the full timestamp , with seconds?

Regards
Muktha
9 REPLIES 9
Suraj K Sankari
Honored Contributor

Re: List with full timestamp of files

Hi Muktha ,
>>Can we display the full timestamp , with seconds?

I think it's not possable to see time stamp with second.

Suraj
Fredrik.eriksson
Valued Contributor

Re: List with full timestamp of files

I believe that ls supports time-styling... something like this might help you.

ls --time-style=+%Y%m%d-%H%M%S -l

According to the manpage it's in standard `date` formating.

Best regards
Fredrik Eriksson
Pete Randall
Outstanding Contributor
Suraj K Sankari
Honored Contributor

Re: List with full timestamp of files

Hi Again,

I thought its not possible to get timestamp with second field. After digging the forum I got a Perl script on this thread see the below link

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1123761&admit=109447626+1245238228339+28353475

Here is the Perl script to listing with second.
perl -e 'foreach(@ARGV){$time=localtime((stat($_))[9]);printf("%-30s%s\n",$_,$time)}' *

Suraj
James R. Ferguson
Acclaimed Contributor

Re: List with full timestamp of files

Hi:

If you use Perl's POSIX module you can format the timestamp in any way you like. The formatting directives for 'strftime()' are the same you know from the shell's 'date' caommand. For example:

# perl -MPOSIX -le 'for (@ARGV) {-e $_ && print join " ",strftime("%b %2d %Y %X",localtime((lstat($_))[9])),$_}' /home/muktha/*

This variation silently ignores non-existent files and directories and shows the mtime of a symbolic link rather than the object of that link. To see the contents of a directory (at one level), specify the argument(s) with the trailing "/*" as my example shows.

Regards!

...JRF...

Regards!

...JRF...
Muktha
Frequent Advisor

Re: List with full timestamp of files

Hi,

Many thanks to all.
Perl command works fine.
As I am not good in Perl, can u please explain me that command?

--timestyle option is not working in my Unix box.

I had one more request , can we list in ascending or descending order

Regards
Muktha
OldSchool
Honored Contributor

Re: List with full timestamp of files

"I believe that ls supports time-styling... something like this might help you.

ls --time-style=+%Y%m%d-%H%M%S -l

According to the manpage it's in standard `date` formating."

only according to the *Linux* man page. not the "unix" man page. the giveaway is the "double-minus" in the option.

if you really want that option, you can install gnu coreutils
James R. Ferguson
Acclaimed Contributor

Re: List with full timestamp of files

Hi (again) Muktha:

# perl -MPOSIX -le 'for (@ARGV) {-e $_ && print join " ",strftime("%b %2d %Y %X",localtime((lstat($_))[9])),$_}' /home/muktha/*

This uses the POSIX module to offer 'strftime()' formating as I noted. The script examines every argument passed (as held in @ARGV). For each element which exists (-e) a stat() system call is made to obtain the modification timestamp (field-9 of the stat() structure). Since this timestamp is in Epoch seconds, we convert it to your localtime zone's interpretation, format its elements with strftime() and print the formatted string together with the element we just extracted from the argument list (@ARGV).

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: List with full timestamp of files

Hi (again) Muktha:

> I had one more request , can we list in ascending or descending order?

OK, I missed that. One very simple way to accomodate that is this variation (which adds the Epoch timestamp in the first column. Now you can pipe the output to a sort and order the results in descending or ascending order:

# perl -MPOSIX -le 'for (@ARGV) {-e $_ && ($t=(lstat($_))[9]) && print join " ",$t,strftime("%b %2d %Y %X",localtime($t)),$_}' ./* |sort -krn1,1

...which would present the contents of the current directory's files (./*) in descending age order.

Regards!

...JRF...