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 Chriss 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 X minutes old 1

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I need to make a script that can check if a file is over X number og minutes old. It has to account for midninght and newyears. I'v tryede making it in ksh but couldn't figure out how to get it to work probaly, and someone told me that perl would be mutch better at it. - I don't know thats mutch about pearl.

I working on a HP-unix server.

/Larshg


 
You can use [tt]stat $file[/tt] to get a list of various information about [tt]$file[/tt]. The 10th element is the last modified time in seconds since the epoch. (9th is last access time in seconds since, 11th is inode change time in seconds since the epoch [which should be similar to creation time, but is not actually... I don't know the precise differences.])

[tt]if ((time - (stat $file)[9]) > ($minutes * 60)) {
print "File was last modified more than $minutes ago\n";
}
[/tt]
 
Thanks it works great

Can you tell me if I need to worry about midnight or newyears? - are there any senarios where this script will have problems?

/Lars
 
No problems with midnight or New Years, since the values are just seconds since January 1, 1970 (or something along those lines.)

The only potential problem would be related to daylight savings time. When the clock goes back an hour, the file would be an hour younger than it was (or an hour older when the clock goes the other way.)
 
Rosenk,

Actually - I think your code will work just fine over a daylight saving time transition. Dates and times in UNIX are stored as the number of seconds since 1st Jan 1970, without regard to daylight saving. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top