i need to watch files that fall under a directory.I have coded the below script in perl . but it is not doing what i want .
- whenever a file or files arrives , it has to do a movement .
- And then it has to keep watching files again.
the script should be running in background.
#!/usr/bin/perl use warnings; use File::Copy qw(move); $src_dir = '/root/prasanna/dir'; $tgt_dir = '/root/prasanna/dir/dir1'; while (true) { opendir( DIR, "/root/prasanna/dir" ) or die "Cannot open /root/prasanna/dir: $!\n"; my @Dircontent = readdir DIR; close DIR; my $items = @Dircontent; if ( $items > 2 ) { print "files available"; while ($items) { print $items; move $src_dir. '/' . $items, $tgt_dir . '/' . $items; unlink $items; } } else { sleep 50; } }
The problem with the above code is 1. the if statement keeps on printing the 'files available' . goes on infinite loop , it doesnt watch for files again .even if i do operations on file, i dont knw how to make it look for files again. 2. the script doesnt run in background .
any help is highly appreciated . thanks beforehand.!