ファイルだけじゃなくてディレクトリも監視できます
inotifyはファイルだけじゃなくてディレクトリもウォッチできて、配下にファイルが作られたり消されたりなどなどを監視できます。
$ watch-it.pl ~/tmp (ls) tmp - IN_ISDIR IN_OPEN tmp - IN_ISDIR IN_CLOSE_NOWRITE IN_CLOSE (echo test > foo) tmp/foo - IN_CREATE tmp/foo - IN_OPEN tmp/foo - IN_MODIFY tmp/foo - IN_CLOSE_WRITE IN_CLOSE (echo test >> foo) tmp/foo - IN_OPEN tmp/foo - IN_MODIFY tmp/foo - IN_CLOSE_WRITE IN_CLOSE (touch foo) tmp/foo - IN_OPEN tmp/foo - IN_ATTRIB tmp/foo - IN_CLOSE_WRITE IN_CLOSE (chmod 644 foo) tmp - IN_ISDIR IN_OPEN tmp/foo - IN_ATTRIB tmp - IN_ISDIR IN_CLOSE_NOWRITE IN_CLOSE (rm foo) tmp/foo - IN_DELETE (mkdir bar) tmp/bar - IN_ISDIR IN_CREATE (chmod 775 bar) tmp - IN_ISDIR IN_OPEN tmp/bar - IN_ATTRIB IN_ISDIR tmp - IN_ISDIR IN_CLOSE_NOWRITE IN_CLOSE (rmdir bar) tmp/bar - IN_ISDIR IN_DELETE
#!/usr/bin/env perl # Time-stamp: <2007-12-15 01:20:57 JST, hirose31> use strict; use warnings; use utf8; use Carp; use Linux::Inotify2; my %MASK; { no strict 'refs'; $MASK{$_} = &{"Linux::Inotify2::$_"} for @Linux::Inotify2::constants; delete $MASK{IN_ALL_EVENTS}; } sub pretty_mask { my($mask) = @_; my @mname; @mname = grep { $mask & $MASK{$_} } keys %MASK; join ' ', @mname; } my $n = Linux::Inotify2->new or croak $!; $n->watch($ARGV[0], IN_ALL_EVENTS) or croak $!; while (1) { my @ev = $n->read; if (@ev > 0) { for my $e (@ev) { printf "%-12s - %s\n", $e->fullname, pretty_mask($e->mask); } } else { croak "read error: $!\n"; } } __END__ # for Emacsen # Local Variables: # mode: cperl # cperl-indent-level: 4 # indent-tabs-mode: nil # coding: utf-8 # End: # vi: set ts=4 sw=4 sts=0 :