FILENAME="<filename>"
CHANGE=$( date +%s --date="$( stat $FILENAME | grep ^Modify: | sed 's/^Modify: //' )" )
NOW=$( date +%s )
DIFF=$( echo "$NOW - $CHANGE" | bc )
if test $DIFF -gt 3600
then
echo "The file is old"
fi[/tt] //Daniel
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.
*/
# See if it's over an hour old...
if (( DIFF > 3600 ))
then
print "${FILENAME} is over 1 hour old!"
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.
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.
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
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.
"%s" 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 "yes"
next
}
$8 ~ /^[0-9]+:[0-9]+$/ {
FILEMONTH=$6
FILEDATE=$7
FILETIME=$8
gsub(":","",FILETIME)
if (FILEMONTH == CURMONTH && FILEDATE == CURDATE) {
if (FILETIME > CURTIME + 100) {
print "yes"
} else {
print "no"
}
} else {
print "yes"
}
next
}
{
print "not sure"
}'[/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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.