Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking if a file is over an hour old

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I need to make a script that will check if a file is over an hour old.

meening that I need to deduct the time the file was updatede and `date`

I do not want to use SQL - but it needs to account for midnight.

Thanks
/Lars
 
This should work:
[tt]#!/bin/bash

FILENAME=&quot;<filename>&quot;
CHANGE=$( date +%s --date=&quot;$( stat $FILENAME | grep ^Modify: | sed 's/^Modify: //' )&quot; )
NOW=$( date +%s )
DIFF=$( echo &quot;$NOW - $CHANGE&quot; | bc )
if test $DIFF -gt 3600
then
echo &quot;The file is old&quot;
fi[/tt] //Daniel
 
Hi

on my system the bash shell lies in /usr/local/bin/bash - but appart from that I can't seem to get the stst command to work!

I tryede to locate it by making a find / -name stat - but it can't find it? - can you tell me more about it or if I can do something else?

/Lars
 
What [tt]stat[/tt] does is to print information about a file. If you figure out another way to get the last modified date for a file, just change the [tt]CHANGE[/tt] line. //Daniel
 
Here's something I wrote for a similar need. It's a short C program that just outputs the file's last modification time in whole seconds from the Unix base date.
[tt]
/*
What: filedate.c
Who: Sam Pawley (aka SamBones)
When: 22-Nov-2002
Why: This is to display the datestamp of a file for a script
to use in file age comparisons.
*/

//#define DEBUG DEBUG

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char ** argv)
{
struct stat filestats;

if ( argc != 2 )
{
fprintf(stderr, &quot;filedate: incorrect number of parameters!\n&quot;);
return( -1 );
}

if( stat(argv[1], &filestats) )
{
fprintf(stderr, &quot;filedate: Couldn't get file stats for %s!\n&quot;, a
rgv[1]);
return( -1 );
}

#ifdef DEBUG
printf(&quot; File Name: %s\n&quot;, argv[1]);
printf(&quot; Size: %ld\n&quot;, filestats.st_size);
printf(&quot; Last Access Time: %ld\n&quot;, filestats.st_atime);
printf(&quot; Last Mod Time: %ld\n&quot;, filestats.st_mtime);
printf(&quot;Last Stat Chg Time: %ld\n&quot;, filestats.st_ctime);
#endif

printf(&quot;%ld\n&quot;, filestats.st_mtime);

return(0);
}

/* EOF: filedate.c */
[/tt]
Then, in your script you can do something like...
[tt]
#!/bin/ksh

# Get the current date/time stamp
TMPFILE=/tmp/tmp.$$.${RANDOM}
touch ${TMPFILE}
CURRDATE=$(filedate ${TMPFILE})
rm -f ${TMPFILE}

# Get the file in question's date/time stamp
FILEDATE=$(filedate ${FILENAME})

# Figger the difference
typeset -i DIFF
(( DIFF = CURRDATE - FILEDATE ))

# See if it's over an hour old...
if (( DIFF > 3600 ))
then
print &quot;${FILENAME} is over 1 hour old!&quot;
fi
[/tt]
I use this in a home build file cleanup and archive utility that will gzip a file after it's a certain age.

You could also change the program to return the last atime, which is the last time the file was accessed, even if it wasn't changed.

You could also change the program to grab the system time and just output the file's age in seconds. Then you wouldn't need the little temp file to get the current date.

Hope this helps!
 
Hi

The time stamp I been toald to look after is the time stamp unix gives til file when it has been generatede - ex.
-rw-r----- 1 cgi dev 3585176 Feb 14 21:56 NOKIA_ABSMS07_20030214155648_0014.DAT

The 'Feb 14 21:56' should be deductede from `data` - but I have trouble deducting the to time stamps if I have to account for midnight and newyear without using SQL - I heard that there is a way to convert the time stamps into the amount of secound from 1980 to the time stamp.

/Lars

 
That's what both SamBones and my script does, it gets the age of the file in seconds since January 1st, 1970, then it gets the current time in the same format, and then it substracts the age from the current time. If the resulting value is greater than 3600, your file is older than an hour. //Daniel
 
Okay

Can you tell me how to get the timestamp 'Feb 14 21:56' comvertede into seconds since January 1st, 1970?

And I can't seem to make the NOW=$( date +%s ) to work - this is the error
date: bad format character - s



 
Did you try using the script and program that SamBones posted? It's working just fine on all my systems.

The [tt]%s[/tt] in the [tt]date[/tt] arguments means to output a UNIX timestamp (seconds since January 1st, 1970). //Daniel
 
SamBones script is no good to me becouse this is gonning to be usede in a monitoring tool - witch executes it remotly on the server, thats meens that I hve to do it in one script - this is also why I realy dont want to use SQL.(and becourse it should be nessary.)

I cant find any thing on %s or s as a parameter for date on my system - I'm working on a HP-unix system.

/Lars
 
&quot;%s&quot; date format isn't available on Solaris either. How about something like this:

[tt]#!/bin/ksh

ls -ld $1 | nawk -v CURTIME=$(date +%H%M) -v CURMONTH=$(date +%b) -v CURDATE=$(date +%d) '
$8 ~ /^[0-9]+$/ {
# 8th column is a year, it must be more than an hour old!
print &quot;yes&quot;
next
}
$8 ~ /^[0-9]+:[0-9]+$/ {
FILEMONTH=$6
FILEDATE=$7
FILETIME=$8
gsub(&quot;:&quot;,&quot;&quot;,FILETIME)
if (FILEMONTH == CURMONTH && FILEDATE == CURDATE) {
if (FILETIME > CURTIME + 100) {
print &quot;yes&quot;
} else {
print &quot;no&quot;
}
} else {
print &quot;yes&quot;
}
next
}
{
print &quot;not sure&quot;
}'[/tt]

The only time I can think of that this won't work is for the hour after midnight; it will think any of yesterday's files are more than an hour old. Change ls -ld to ls -lud to use last accessed times instead of last modified times. I called the script hourold. Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top