Print Unix File Timestamp Accurate Down to Seconds

| 31 Jan 2007 | Posts | 6,945 views

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: , , , , , ,

Share

Related posts:

:, , Subscribe


5 Comments for this entry

  • Jason Ennor

    excellent! thanks.

  • Alex

    Thanks a lot!!!

  • Vikas

    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

  • lokety

    Vikas

    It looks like not only temp_01.dbf is showing the wrong year, but a number of other datafiles are affected. Perhaps you can try running the command after this oracle instance is dismounted and/or closed?

  • Mike Selah

    This works perfectly, however I was wandering if it would be possible to format the date as yyyymmddhrminsec
    ex 201011310400.33
    perl -e ‘foreach(@ARGV){$t =localtime ( ( ( stat ( $_ ) ) [9] ) ); printf(“%-20s %s\n”,$_,$t);}’ *

Leave a Reply

Archives

Be Good

View Teng-Yan Loke's profile on LinkedIn

The Hunger Site

Counter visits since 16 Sep 2006

Technology Blogs - BlogCatalog Blog Directory