Force date display


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Force date display
# 1  
Old 08-01-2007
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  
Old 08-01-2007
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  
Old 08-01-2007
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  
Old 08-01-2007
Quote:
Originally Posted by jim mcnamara
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  
Old 08-01-2007
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  
Old 08-02-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

[Solved] Need help in date display

Hi, I have one hp-ux sytem date display problem when viewed remotely hostA 21: date Fri Jul 5 11:18:51 SST 2013 hostA 22: remsh hostB -l username -n date 2013年07月05日 11時32分27秒 But on actual system it display correctly:confused: hostB 21: date Fri Jul 5 11:40:33 SST 2013 (4 Replies)
Discussion started by: jorendain
4 Replies

2. Shell Programming and Scripting

How to display a date, 30 days from the current date?

Well guys, I know the right syntax for displaying the current date is $(date). However, I am planning to send emails to some customers which displays their subscription date, and then the expiry. The expiry being 30 days from the current date. What would the right syntax be? (6 Replies)
Discussion started by: xxxx
6 Replies

3. UNIX for Dummies Questions & Answers

how to display date with filename??

Hi i am currently using below command in shell script and calling it from cron job. This is to take daily backup with the date as suffix to the file.When i try i unable to get the date suffixed to the backup file. Please help me as to wt's wrong in this or how to get the date added to the... (5 Replies)
Discussion started by: abhi_n123
5 Replies

4. UNIX for Dummies Questions & Answers

Date format Display Help

I have tried various arguments to get the date display as "Mar 10". I have tried date +"%c" -------> Wed Mar 10 11:51:21 EST 2010 date +"%b%d%Y_%H%M%S" --------> Mar102010_115121 date +"%b%d" -------> Mar10 date +"%t%b%e" ... (3 Replies)
Discussion started by: moveaix
3 Replies

5. Shell Programming and Scripting

Display the last five dates from the given date

Hi all, In Oracle we have got sysdate -1 to find the previous date. Is there any similar way to display date in unix shell scripting? Kindly help me to display the last five dates from the given date Thanks, Geetha (11 Replies)
Discussion started by: iamgeethuj
11 Replies

6. UNIX for Dummies Questions & Answers

Display particular date files

Hi, How to display particular date's files in Unix? e.g. I want to display april 30th 2009 files only. Regards, Venkatesh. (3 Replies)
Discussion started by: venkatesht
3 Replies

7. Shell Programming and Scripting

display filename with date

Hi buddies, I have a doubt. I want to display filename with date in the following format.Is there any way to do this. Kindly give me the solution. I want to display the result in the following manner. test1.txt 03/28/2008 testlog.log 02/20/2008 Please let me know one solution how to do... (1 Reply)
Discussion started by: pstanand
1 Replies

8. Shell Programming and Scripting

DATE display Problem

I have write echo `date +%y%m%d` but it will give 070529. why I am not getting 070530. I have Make any syntactical error? please help. (2 Replies)
Discussion started by: rinku
2 Replies

9. Shell Programming and Scripting

Date display

Hello, Is there any easy way to write a script to display the date for the previous day? I have a script which queries a sybase database, and pull the data for certain time period. I'd like to know how you can get the day for previous day, something like: date - 1! Thanks, Frank (2 Replies)
Discussion started by: FrankC
2 Replies

10. Solaris

display date n Time

Hi Friends, Can any one guide me regarding 'Display the date and time' command other than the command 'date' thanks n regards SsRrIi (1 Reply)
Discussion started by: SsRrIi
1 Replies
Login or Register to Ask a Question