Wednesday, January 31, 2007

Print Unix File Timestamp Accurate Down to Seconds

As most of us know and need, ls -l shows us detailed information about files in a directory.

someone@somewhere:/opt/mozilla/icons $ ls -l
total 32
-rw-r--r-- 1 bin bin 1668 Mar 5 2004 mozicon16.xpm
-rw-r--r-- 1 bin bin 2944 Mar 5 2004 mozicon50.xpm

Here is a single perl command that can print the timestamp of files with full date and time, accurate down to the seconds:

perl -e 'foreach(@ARGV){$t =localtime ( ( ( stat ( $_ ) ) [9] ) ); printf("%-20s %s\n",$_,$t);}' *

Replace the asterisk at the end of the command with any filename or wildcard expression you require.

Technorati Tags: , , , , , ,

3 comments:

Jason Ennor said...

excellent! thanks.

Alex said...

Thanks a lot!!!

BI_PM said...

I run your script as follows:
Perl -e 'foreach(@ARGV){$t =localtime ( ( ( stat ( $_ ) ) [9] ) );printf("%-20s %s\n",$_,$t);}' /db/u/cru/v*/*/*/*.dbf > timestamp.log 2>&1

It prints fine for some and gives me a weird timestamp for others.

Here is the illustration of simple ls –ltr

And then when I run the perl script



alauqpnc010/db/u/cru/v3/system/CRUU1:CRUU1>ls -ltr *.dbf

-rw-r----- 1 dbucru dba 210771968 Mar 3 13:27 users_01.dbf

-rw-r----- 1 dbucru dba 52436992 Mar 3 13:27 tools_01.dbf à correct timestamp

-rw-r----- 1 dbucru dba 3145736192 Mar 4 04:00 temp_01.dbf à Dec 31 timestamp

-rw-r----- 1 dbucru dba 9438240768 Mar 6 14:02 undotbs.dbf

-rw-r----- 1 dbucru dba 524296192 Mar 6 14:02 system_01.dbf

-rw-r----- 1 dbucru dba 524296192 Mar 6 14:02 sysaux01.dbf



Perl script output

/db/u/cru/v3/index/CRUU1/cru_indexes_01.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/system/CRUU1/sysaux01.dbf Fri Mar 6 14:02:33 2009

/db/u/cru/v3/system/CRUU1/system_01.dbf Fri Mar 6 14:02:33 2009

/db/u/cru/v3/system/CRUU1/temp_01.dbf Wed Dec 31 19:00:00 1969 à incorrect Dec 31 timestamp

/db/u/cru/v3/system/CRUU1/tools_01.dbf Tue Mar 3 13:27:36 2009 à correct timestamp

/db/u/cru/v3/system/CRUU1/undotbs.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/system/CRUU1/users_01.dbf Tue Mar 3 13:27:36 2009

/db/u/cru/v3/table/CRUU1/cru_crystadata_01.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/table/CRUU1/cru_data_01.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/table/CRUU1/cru_dellogimage.dbf Tue Mar 3 13:27:36 2009

/db/u/cru/v3/table/CRUU1/result_datapart01.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/table/CRUU1/result_datapart02.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/table/CRUU1/result_datapart03.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/table/CRUU1/result_datapart04.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/table/CRUU1/result_datapart05.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/table/CRUU1/result_datapart06.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/table/CRUU1/result_datapart07.dbf Wed Dec 31 19:00:00 1969

/db/u/cru/v3/table/CRUU1/result_datapart08.dbf Wed Dec 31 19:00:00 1969