BUU has asked for the wisdom of the Perl Monks concerning the following question:

In short, what the title says. I've got a large number of files in directories (of course they're not all in one directory, that would be too easy =/ ) and I'd like to watch and do something useful when ever *any* of them are modified. I suppose I could create a list, store mtimes and loop, but that seems a little ugly and possible inefficient, anyone have a better idea? The solution need only work on linux.
  • Comment on Watching large numbers of files for modifications?

Replies are listed 'Best First'.
Re: Watching large numbers of files for modifications?
by gaal (Parson) on Feb 15, 2005 at 10:07 UTC
Re: Watching large numbers of files for modifications?
by gopalr (Priest) on Feb 15, 2005 at 10:52 UTC
    use File::Find; @ARGV = 'c:\temp'; $filesize=-1; $filename=''; find(\&test, @ARGV); # to get the biggest file size in a directory sub test { if (-f && -s _ > $filesize) { $filesize = -s _; $filename = $File::Find::name; } } print "\nBiggest file"; print "\n============"; print "\n$filename in @ARGV is $filesize bytes long.\n\n\n"; find(\&test1, @ARGV); # to get the Modify Date and Time in a directory print "\nModify Date"; print "\n============\n"; my ($mtime, $fname); sub test1 { $mtime = (stat(_))[9]; $fname = $File::Find::name; print "$fname " . scalar(localtime($mtime)) . "\n"; }
Re: Watching large numbers of files for modifications?
by gellyfish (Monsignor) on Feb 15, 2005 at 10:11 UTC
Re: Watching large numbers of files for modifications?
by holli (Abbot) on Feb 15, 2005 at 11:35 UTC
    You mentioned linux, but somebody might read this and need a solution for Win32. In that case there is Win32-ChangeNotify.


    holli, /regexed monk/

    Update:
    yet another bad joke ;) | | | \ / v
        You mentioned linux, but somebody might read this and need a solution for Win32.
      I thought that Linux was a solution for Win32.

      </smartbutt>

      --
      tbone1, YAPS (Yet Another Perl Schlub)
      And remember, if he succeeds, so what.
      - Chick McGee

Re: Watching large numbers of files for modifications?
by dragonchild (Archbishop) on Feb 15, 2005 at 13:39 UTC
    but that seems a little ugly and possible inefficient

    Ahhh, but is it? In other words, have you tried your idea? I have, and it worked just fine for my needs.

    Update: There's also a solution on CPAN, though it's somewhat hidden. Log::Log4perl::Config::Watch is basically a file watcher. (Maybe we can get the author to re-release this one module as File::Watcher or something ... )

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.