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

Hi, I want to monitor the directory in linux, So as to find the new files added to the directory after i start execution of my perl script. Note: Directory cannot be touched(we cannot create any file through my script), we should just monitor the directory and need to process the new files added.

Thanks in advance.
  • Comment on Find the Files added to the directory in Linux.

Replies are listed 'Best First'.
Re: Find the Files added to the directory in Linux.
by Loops (Curate) on Jul 05, 2013 at 06:12 UTC

    There is more than one way to skin this cat. Here is one way using the Linux inotify facility

    See Linux::Inotify for details

    use strict; use warnings; use Linux::Inotify; my $notifier = Linux::Inotify->new(); my $watch = $notifier->add_watch('/directory/to/monitor', Linux::Inoti +fy::CREATE); while (1) { for my $event ($notifier->read()) { print "New file named: " . $event->fullname() . "\n"; } }

      Loops,

      I would recommend you include a 'sleep' or 'usleep' to your 'while' loop. Otherwise you will eat up a lot of the computer!

      Regards...Ed

      "Well done is better than well said." - Benjamin Franklin

        No, sleep and usleep would be counter productive and just slow detection down. The whole beauty of iNotify is that the process sleeps until an event happens, and then its woken by the kernel and passed the event structure. The code as is takes next to zero system resources.

Re: Find the Files added to the directory in Linux.
by kcott (Archbishop) on Jul 05, 2013 at 08:05 UTC

    G'day venky4289,

    I haven't used it (so this isn't a recommendation) but recently I saw a reference to File::Monitor which may do what you want.

    -- Ken