use warnings;
use strict;
use Linux::Inotify2;
my $target = defined $ARGV[0]?$ARGV[0]:'.';
print STDERR "Watching $target...\n";
my $in2 = Linux::Inotify2->new();
die "Inotify2: $!" if (! defined $in2 );
$in2->watch ($target, IN_ALL_EVENTS) or die "watch: $!";
while (1) {
for my $e ($in2->read()) {
print "$e->{name} ".$e->mask."\n";
translate_event($e);
}
}
sub translate_event
{
my ($ev) = @_;
my $action = $ev->fullname;
$action .= " was read" if ($ev->IN_ACCESS);
$action .= " opened for writing was closed" if ($ev->IN_CLOSE_WRITE
+);
$action .= " not opened for writing was closed" if ($ev->IN_CLOSE_N
+OWRITE);
$action .= " created in watched directory" if ($ev->IN_CREATE);
$action .= " deleted from watched directory" if ($ev->IN_DELETE);
$action .= "Watched file/directory was itself deleted" if ($ev->IN_
+DELETE_SELF);
$action .= " was modified" if ($ev->IN_MODIFY);
$action .= "Watched file/directory was itself moved" if ($ev->IN_MO
+VE_SELF);
$action .= " moved out of watched directory" if ($ev->IN_MOVED_FROM
+);
$action .= " moved into watched directory" if ($ev->IN_MOVED_TO);
$action .= " was opened" if ($ev->IN_OPEN);
print "$action\n";
#return $action?$action:' unknown action';
}
Just run the required script (maybe using system) when you detect the change you are interested in. |