file date format | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users 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.

file date format

UNIX for Advanced & Expert Users


Tags
perl, perl shift, shift, shift perl

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-28-2006
ludenso ludenso is offline
Registered User
 
Join Date: Apr 2006
Last Activity: 11 September 2006, 7:51 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
file date format

ls -l doesn't return the date of a file in a numerical format

Is there an other command to retrieve the date of an existing file in one of the following formats YYYYMMDD or MMDDYYY ?
Sponsored Links
    #2  
Old 04-28-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
as such with ls there is no format commands,
you need to format them.
Sponsored Links
    #3  
Old 04-28-2006
ludenso ludenso is offline
Registered User
 
Join Date: Apr 2006
Last Activity: 11 September 2006, 7:51 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Java

-rw-rw-r-- 1 xos xos 0 Apr 28 12:54 test.txt

There are 2 problems with the date information returned by the ls -l command :
1) the month is not indicated numerically 01 .. 12
2) the year is only displayed if it is older than 6 months

These two factors complicate reformating the output of the line above into 20060428
    #4  
Old 04-28-2006
buffoonix buffoonix is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 26 January 2009, 11:26 AM EST
Posts: 145
Thanks: 0
Thanked 1 Time in 1 Post
You need to stat the file yourself by the namesake syscall.
Either write a small C wrapper program or you could write a Perl wrapper which is more effortless

e.g.

Code:
$ perl -MPOSIX=strftime -le 'print strftime("%Y%m%d", localtime((stat shift)[9]))' /etc/passwd
20060414
$ ls -l /etc/passwd
-rw-r--r--  1 root root 2224 Apr 14 18:25 /etc/passwd

Sponsored Links
    #5  
Old 04-30-2006
Santi Santi is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 2 April 2009, 5:45 PM EDT
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

Try passing "--full-time/--time-style" to ls command:

Quote:
--time-style=STYLE
show times using style STYLE: full-iso, long-iso, iso, locale, +FORMAT

FORMAT is interpreted like `date'; if FORMAT is FORMAT1<newline>FORMAT2, FORMAT1 applies to non-recent files and FORMAT2 to recent
files; if STYLE is prefixed with `posix-', STYLE takes effect only outside the POSIX locale.
Here is a little example:

Quote:
$ ls -l --full-time /etc/passwd
-rw-r--r-- 1 root root 1158 2006-04-26 20:49:57.000000000 +0200 /etc/passwd

$ ls -l --full-time /etc/passwd | awk ' { print $6 } '
2006-04-26
Try also with "stat" command:

Quote:
$ stat -c %x /etc/passwd
2006-04-30 15:01:01.000000000 +0200
Read the differences between times in UNIX systems:

http://www.brandonhutchinson.com/ctime_atime_mtime.html

Regards,

--
Santi Saez
Sponsored Links
Closed 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
finding date numeral from file and check the validity of date format manas_ranjan Shell Programming and Scripting 10 11-18-2011 02:58 PM
Changing from Excel date format to MySQL date format figaro UNIX for Dummies Questions & Answers 2 08-11-2009 03:23 PM
convert date format to mysql date format in log file hazno Shell Programming and Scripting 3 03-03-2009 08:05 AM
Format a date in a csv file rpiller Shell Programming and Scripting 5 02-20-2009 01:26 AM
Convert Epoch time format to normal date time format in the same file rk4k Shell Programming and Scripting 3 11-19-2008 09:04 PM



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