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

Files younger than 24 hours old

Status
Not open for further replies.

billbose

Programmer
Jan 15, 2001
66
US
I am trying to find files younger than 24 hours old in the /tmp directory.


-------------------------------
#!/usr/bin/perl -w
use strict;
use File::stat ;

opendir(D, "/tmp") or die $!;
my $time = time();

while (my $f = readdir(D)) {
next if $f =~ /^\./;
next unless $f =~ /^log/ ;

my ($atime, $mtime, $ctime) = (stat($f))[8..10];
my $age_hours = ($time - $mtime) / 3600 ;

next unless $age_hours < 24 ;
print "---> $f \n" ;


}
closedir(D);

---------------------------

The above script simply doesnt work for me.
 
It's because of File::stat. You dont need this module for stat function. This module's default exports override the core stat() and lstat() functions, replacing them with versions that return "File::stat" objects. Look at
to know more about this.
Remove the 'use File::stat' if you are not using it anywhere and it should work.
 
HW?
Code:
#!/usr/bin/perl
use strict;
use File::stat;
opendir(D, "/tmp") or die $!;
while (my $f = readdir(D)) {
    if (($f =~ /[^\.].*?log$/) && ((time() - ${stat($f)}[9]) > 86400))
    { print "$f\n"; }
}
closedir(D);

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Seeing as you're on a *nix system, there shouldn't actually be any need to write a Perl script for this. It'd be easier to just use `find'.

find /tmp -mtime -1

That will recurse into subdirectories of /tmp as well. If you want only files and directories within /tmp itself, use:

find /tmp -mtime -1 -maxdepth 1

If you only want files (not directories):

find /tmp -mtime -1 -maxdepth 1 -type f
 
foreach( glob("/tmp/log*") ){
print "$_\n" if ((time - (stat($f))[9]) > 86400));
}

So now you have some to choose from ;)


[ponder]KrK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top