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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I tell if a file is more than 15 minutes old? 1

Status
Not open for further replies.

pjb

Programmer
May 1, 2001
148
US
I need a way to determine if a given file has been created or modified in the last 15 minutes. is there a way to do this in shell?
 
Sorry PHV, but your solution only takes into account days and the requirement is "in the last 15 minutes":

1) touch MODIFIES the timestamp of a file.
2) find only FIND day periods.
3) also gives days only.

pjb, try this function:

#!/bin/ksh
#
chk_time() {
# Parameters:
fn=$1 # File to check
mi=$2 # Minutes to check
sDte="$(date|cut -c5-14)"
sMin="$(date|cut -c15-16)"
tOK=$(ls -alp $fn|grep "$sDte"|wc -l)
if [ tOK -ne 1 ]
then
return 1
fi
fMin=$(ls -alp $fn|awk '{print substr($8,4);}')
(( tOK = sMin - fMin ))
if [ tOK -gt mi ]
then
return 1
fi
return 0
}
# Main proc:
file=/tmp/MyFile
chk_time $file 15
rc=$?
if [ rc -gt 0 ]
then
echo "No way Jose, $file is gt 15 min."
else
echo "OK, $file is younger than 15 min."
fi

NOTE: May not work around midnight.





----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
If you've got perl, here's a starting point:

Code:
#!/usr/bin/perl

$file = shift;

$fifteen_mins = ( 15 / 1440 );

if ( ( -M $file ) < $fifteen_mins )
{
print "$file is new or modified in last 15 minutes\n";
}
else
{
print "$file is older than 15 minutes\n";
}

"-M" returns the age of the file at script startup, in days. Fifteen minutes is 15/1440ths of a day.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Yea, perl would be a better solution, my fuction stinks -- doesn't work when changing hours.
Good luck!


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
LKBrwnDBA
1) touch MODIFIES the timestamp of a file.
use touch to create a file with timestamp equal to current time - 15 minutes
2) find only FIND day periods.
false
use find ! -newer the previous touched file
3) also gives days only.
I agree, but it demonstrate how (manipulating TZ) to get a offseted timestamp relative to the current time




Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Another perl way:
Code:
#!/usr/bin/perl -w
use File::Find;
use File::stat;
$now = time;
$offset = 15 * 60;
sub wanted {
    if (-f) {
        $mod = stat($_)->mtime;
        if($now - $mod >= $offset) {
            printf "%s %s\n", $File::Find::name, scalar(localtime($mod));
        }
    }
}
find ( \&wanted, "." );
Cheers, Neil
 
Or in more terse form:
Code:
#!/usr/bin/perl -w
use File::Find;
use File::stat;
$now = time;
$offset = 15 * 60;
find ( sub { print "$File::Find::name\n" if (-f && ($now - stat($_)->mtime >= $offset)); }, ".");
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top