The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
display filename with date pstanand Shell Programming and Scripting 1 03-28-2008 08:35 AM
DATE display Problem rinku Shell Programming and Scripting 2 05-30-2007 01:23 AM
Date display FrankC Shell Programming and Scripting 2 05-01-2006 04:43 PM
display date n Time SsRrIi SUN Solaris 1 03-16-2006 05:48 AM
PHP:display by date perleo Shell Programming and Scripting 3 08-13-2003 02:29 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-01-2007
littlet05 littlet05 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 2
Force date display

When doing a list with date info, 'ls -l', is there a way to force the dates to not change over time (go from hour:min to year) after 6 months or whatever it is?

If not, I would like to know how to replace that column with the current year if it is in the hour:min format and leave it unchanged otherwise. So far I figured I can pipe the list to a grep for the lines with colon's, as paths wont have it, then i can cut the current year from date, but I'm not sure how to replace that field with the year.

Any help is appreciated

P.S. I'm using the Korn Shell
  #2 (permalink)  
Old 08-01-2007
awk awk is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
Here is a C program I did one time because I need HH.MM.SS of last maintained - could be modified to so what you want.

You are on your own for questions, just sharing some code with you.


cat filedate.c
/*
filedate.exe filename

Author: XXXXXX
Date: May, 2003

The purpose of this program is to be given a file name and return
the date and time of creation in the format YYYYMMDD hh:mm:ss

This time can be used for sybase SQL. Oracle may require changes

Limited error checking is performed.

Risks to this:
1) Max date is 2038 - so 35 years from now,this will break.
*/


#include <stdio.h> /* for printf */
#include <stdlib.h> /* for printf */
#include <sys/types.h> /* for stat */
#include <sys/stat.h> /* for stat */
#include <time.h> /* for ctime */
#include <errno.h> /* for errno */

main(argc,argv)
int argc;
char *argv[];
{
int iofd, status;
struct stat x;
struct tm y;

if ( argc != 2 )
{
printf("Usage is filedate.exe <filename>\nAborting run\n");
exit(1);
}

if ( stat(argv[1], &x) != 0 ) /* see stat(2) */
{
printf("Could not get the information on %s\n", argv[1]);
printf("Errno is set to %d - see list in sys/errno.h\n", errno);
printf("Aborting run\n");
exit(2);
}

localtime_r(&x.st_mtime, &y); /* see ctime(3C) */

/*

Returns # of years since 1900 - so we have to add 1900 to figure
Also, # of months since January - again, add 1 to figure

Don't know if we need AM/PM indicator, or will Military time be OK
*/

printf("%04d%02d%02d %02d:%02d:%02d\n", y.tm_year + 1900,
y.tm_mon + 1,
y.tm_mday,
y.tm_hour,
y.tm_min,
y.tm_sec);
}
  #3 (permalink)  
Old 08-01-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,800
I think you may not want that - what happens in February when you have files from December?

This gives you a file date - you can add code to work it into your script as a function

Code:
#!/bin/ksh
# filetimes
filetime()
{
    perl -e '
         $mtime=(stat $ARGV[0])[9];
         @tarray = localtime( $mtime );
         $month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$tarray[4]];
         printf("%02d %s %4d", $tarray[3], $month, $tarray[5] + 1900 );

         ' $1
}

dt=$(filetime somefile)
echo "$dt"

  #4 (permalink)  
Old 08-01-2007
littlet05 littlet05 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 2
Quote:
Originally Posted by jim mcnamara View Post
I think you may not want that - what happens in February when you have files from December?
So what you are saying is, is that there is no changeover on new years to make all of the previous years files just have the day, month, year? Which means a file created in December is listed in february, it will contain the month, day, and time as if it was from the current year, even though that would be impossible? That's unfortunate.
  #5 (permalink)  
Old 08-01-2007
awk awk is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
From "man ls"
(Lower case L) Displays the mode, number of links, owner, group, size (in bytes), and time of last modification for each file. If the file is a special file, the size field contains the major and minor device numbers. If the time of last modification is greater than six months ago, the time field is shown in the format month date year where as files modified within six months the time field is shown as month date time format.

Notice how nothing says current year.
  #6 (permalink)  
Old 08-02-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,800
maybe if you explain what you are ultimately trying to do, it would help. It probably is not reformatting ls displays. But doing soemthing with file times.

ls displays -- They've been like that for at least 20 years and nobody has complained that I know about.
Closed Thread

Bookmarks

Tags
mtime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0