This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 14th, 2012  |  syntax: Perl  |  size: 0.91 KB  |  views: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. use Linux::Inotify2;
  7.  
  8. my $notifier = new Linux::Inotify2 or die "Unable to create new inotify object: $!";
  9. my $mask = IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVED_FROM | IN_MOVED_TO | IN_MOVE_SELF;
  10.  
  11. do_dir(".");
  12.  
  13. while() {
  14.    my @events = $notifier->read;
  15.    unless(@events > 0) {
  16.       print "read error: $!";
  17.       last;
  18.    }
  19.    foreach my $event ( @events ) {
  20.       print $event->fullname." was created\n" if $event->IN_CREATE;
  21.       print $event->fullname." was deleted\n" if $event->IN_DELETE;
  22.       do_dir($event->fullname) if $event->IN_CREATE and -d $event->fullname;
  23.    }
  24. }
  25.  
  26. sub do_dir {
  27.    my $dir = shift;
  28.    $notifier->watch($dir, $mask);
  29.    opendir(my $dh, $dir) or die "Could not open dir $dir: $!";
  30.    while(readdir($dh)) {
  31.       next if $_ eq "." or $_ eq "..";
  32.       do_dir($dir."/".$_) if -d $dir."/".$_;
  33.    }
  34.    closedir($dh);
  35. }
clone this paste RAW Paste Data