If the plan is to run a script at some interval (e.g. via your crontab), and you just want to monitor a single directory (not recursively watching all existing subdirectories), something like this would be pretty quick:
use Croak qw/croak/; # (update; don't forget this line, if making a m +odule) sub check_new_paths { my ( $chkpath, $reflist ) = @_; # path to monitor, list of prev. + contents my %old_list; if ( -f $reflist ) { open( LST, "<", $reflist ) or croak( "Can't read $reflist: $!" + ); %old_list = map { chomp; $_ => undef } <LST>; close LST; } if ( ! -d $chkpath ) { croak( "$chkpath is not a directory" ); } my @new_list = grep { -d } <$chkpath/*>; # NB: file glob does not catch "$chkpath/.any-dot-initial-dirnam +e" my @added_list; if ( keys %old_list ) { for my $p ( sort @new_list ) { push @added_list, $p unless exists( $old_list{$p} ); } } else { warn sprintf( "Creating new list %s with %d items\n", $reflist, scalar @new_list ); } open( LST, ">", $reflist ) or croak( "Can't write $reflist: $!" ); print LST "$_\n" for ( @new_list ); close LST; return @added_list; }
If you need to recursively check subdirs within the target path, you'll want to check out one of the various "File::Find" modules -- or just do something like:
my @new_list = split /\x0/, `find $chkpath -type d -print0`;
(which will also give you subdir names that begin with "."). -- Update: I'm sure you can get File::Glob to list dot-initial file names, too, but "that is left as an exercise..." ;)

But if you can get Linux::Inotify to work (shouldn't be too hard), it'll probably include better, more general tools that you'll want to use.


In reply to Re: monitoring directory for new subdirectories by graff
in thread monitoring directory for new subdirectories by incognito129

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.