It is currently Sat, 19 Apr 2014 01:47:44 GMT



 
Author Message
 Calculate age of a file?
I am writing a script I'd like to report the age of a file. To do
this, I need to get both the system date and the date of a file, then
do some simple math. The simple math, I can handle. The rest is
tricky,

 Is there a way to get the system date and a file date in a form I can
readily do math on? I have imagined already an elaborate set of case
statements replete with cuts that will convert  "Dec   4 14:29:18 CST
2003" into a more managable 731443, but I'm hoping there is a shorter
toute to victory. Thanks in advance.



 Tue, 23 May 2006 04:34:19 GMT   
 Calculate age of a file?

The 'date' command is the usual way to do this.

The crucial question here is /which/ "file date" do you want? Do you want the
ctime or the mtime or the atime of the file? Remember, /none/ of the times
represent the /creation/ time of the file.

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')



 Tue, 23 May 2006 04:47:03 GMT   
 Calculate age of a file?
On Thu, 04 Dec 2003 20:47:03 GMT, Lew.Pitc...@td.com (Lew Pitcher)
wrote:

I know about date, but that returns "Thu Dec   4 16:54:34 CST 2003"
requiring me to tokenize it (which is annoying in sh since there are
no arrays), then convert the relevant components to compatible numeric
values. I can do it that way, of course, but it seems like there is a
better way.

I am looking for the date returned by an l or an ls -l. But again, I
know how to get it that way. I was hoping there was a simpler way to
acquire and compare dates. Perhaps that's wishful thinking, though...



 Tue, 23 May 2006 07:05:29 GMT   
 Calculate age of a file?

set `date "+$Y %m %d"`
year=$1
month=$2
day=$3

    Or:

eval `date "+year=$year month-%m day=%d"`
echo $year
echo $month
echo $year

   If you have it, use the GNU stat command.

   Otherwise:

set x `ls -l FILE`
month=$7
day=$8
year=$9

   Note that with files less then 6 months old, $9 contains the time
   of last modification. (SunOS doesn't print the group, so it's
   positions are offset by -1.)

   With GNU ls:

set x `ls --full-time FILE`
IFS=-
set $7
year=$1
month=$2
day=$3

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



 Tue, 23 May 2006 07:33:47 GMT   
 Calculate age of a file?
In article <ib5vsvsmtmgq4nf62e2h53o3622rftg...@4ax.com>,

If you don't find a better route, you could use this (if you have ksh):

ftp://ftp.armory.com/pub/lib/ksh/days

For example:

. days
set -- $(TZ=0 ls -l somefile)
lDate2unixtime $6 $7 $8
curtime
file_age=$((curtime-lDate2unixtime))

        John
--
John DuBois  spce...@armory.com  KC6QKZ/AE  http://www.armory.com/~spcecdt/



 Tue, 23 May 2006 09:19:30 GMT   
 Calculate age of a file?

Try this little piece of C code, which will display the time_t
values of the modification times of the input files.

The time_t is the number of seconds since 1970 Jan 1.

Once you get the time_t values, you can do the math to
convert the difference to # of days.

$ cat timet.c
#include <sys/stat.h>
int main (int argc, char **argv)
{
  int i;
  struct stat buf;
  if ( argc > 1 ) {
    for (i=1; i<argc; i++ ) {
      if ( stat(argv[i], &buf) == 0 )
        printf ("%30s \t %d \t %s", argv[i], buf.st_mtime,
ctime((time_t *)&(buf.st_mtime)));
    }
  }

$ gcc -o timet.exe timet.c

$ ./timet timet*
                       timet.c   1070647128      Fri Dec  5 09:58:48 2003
                     timet.exe   1070647135      Fri Dec  5 09:58:55 2003



 Wed, 24 May 2006 02:07:32 GMT   
 Calculate age of a file?

Here is a second version that can do the math for you.

$ cat file-age.c
#include <sys/stat.h>
int main (int argc, char **argv)
{
  int i;
  struct stat buf;
  time_t timet_diff;
  time_t time_now = time(NULL);
  if ( argc > 1 ) {
    for (i=1; i<argc; i++ ) {
      if ( stat(argv[i], &buf) == 0 ) {
        timet_diff = time_now - buf.st_mtime;
        printf ("%30s \t %5d day(s)-old  %s", argv[i], timet_diff / 86400,
ctime((time_t *)&(buf.st_mtime)));
      }
    }
  }

$ gcc -o file-age.exe file-age.c

$ ./file-age *
...
                        oracle      51 day(s)-old  Tue Oct 14 16:13:49 2003
                           sys     261 day(s)-old  Tue Mar 18 16:51:36 2003
...



 Wed, 24 May 2006 03:42:00 GMT   
 Calculate age of a file?
I cut and pasted that into my RH 7.3 box and gcc complains:
$ gcc -o fileage fileage.c
fileage.c: In function `main':
fileage.c:6: `time_t' undeclared (first use in this function)
fileage.c:6: (Each undeclared identifier is reported only once
fileage.c:6: for each function it appears in.)
fileage.c:6: parse error before `timet_diff'
fileage.c:11: `timet_diff' undeclared (first use in this function)
fileage.c:11: `time_now' undeclared (first use in this function)
fileage.c:12: parse error before `)'

the previous version didn't define it, but this version looks like it does
not sure of the error

John

news:cc5Ab.11833$d35.4721@edtnps84...



 Fri, 26 May 2006 06:09:22 GMT   
 Calculate age of a file?
[please don't top post]

    Add this at the top of the file (see above):

#include <time.h>

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



 Fri, 26 May 2006 06:14:00 GMT   
 
   [ 9 post ] 

Similar Threads

1. Easy method to calculate the age of a file

2. Calculating age in shell/sed script

3. Passwd Aging - What determines "age"??

4. Calculate total size of user files

5. Swap File and Calculating the Necessary Size

6. How to age messages files

7. Script to select files based on age.

8. Need file for password aging

9. How to determine a file "age"


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software