#!/usr/bin/perl use warnings; use strict; my $dir_to_monitor = 'your/directory/here'; chdir $dir_to_monitor; # We'll wait for 60 seconds between invocations my $sleep_time = 60; # We deem a file 'unchanging' if its size hasn't changed since # the last time we checked. Thus, these hashes hold the file # sizes from our last time through the loop: my %unchanging_file_sizes; # Already been sftp'd elsewhere my %changing_file_sizes; # Files that are potentially growing # Holds the state of the loop. This is set to zero when the file # named 'stop_sftp_checking' is found. my $still_running = 1; while ($still_running) { # Check all the files in the directory FILE: foreach my $filename (glob '*') { # Ignore the file if it hasn't changed next FILE if (-s $filename == $unchanging_file_sizes{$filename}); # Check files that were changing the last time around to see # if they're done changing. if (-s $filename == $changing_file_sizes{$filename}) { sftp_file($filename); delete $changing_file_sizes{$filename}; $unchanging_file_sizes{$filename} = -s $filename; next FILE; } # At this point we can be sure that either the file is new # or the file size has changed since we last checked. Either # way, make sure we check it on our next go 'round the loop: delete $unchanging_file_sizes{$filename}; $changing_file_sizes{$filename} = -s $filename; # Finally, our exit condition. If we find a file named # 'stop_sftp_checking' then quit after handling the last file. $still_running = 0 if $filename eq 'stop_sftp_checking'; } # Wait and repeat sleep $sleep_time; }