anonymized user 468275 has asked for the wisdom of the Perl Monks concerning the following question:
I have a script whose job is nominally to find all files whose modification took place between two exact times. File::Find seemed appropriate for the job, but before making the script more flexible to handle future requirements, I wanted to remove the annoyance of it trying to cd into protected areas such as lost+found and generating error messages in the middle of its output. Of course, I could run it with script 2>/dev/null, but let's assume that for the future requirements this will be unnacceptable - the code either has to suppress any error messages that happen in the find mechanism or prevent them happening by controlling where it actually cds.
If I could get away from needing the 'wanted' subroutine (I can't unless I allow find to get all files and then postprocess them which rather defeats the point of using find instead of my own traversal routine anyway), I could always use the no_chdir option to prevent the behaviour. But the syntax of find doesn't allow both %options and \&wanted. So, learned monks, I should be grateful to know what you would recommend.
Thanks in advance,
Simon.
#!/usr/bin/perl use File::Find; use Time::Local; use strict; use vars qw ( @times ); use warnings; my @trees = ( '/home/ftp/pub/', $ENV{ AC_SYSTEM }, $ENV{ AC_WORKDIR } ); # the two time limits between which files are to be searched local @times = ( timelocal(0,0,15,15,7,105), timelocal(0,0,18,16,7,105) ); find( \&wanted, @trees ); sub wanted { # $_ contains the filename without path /^\./ and return; # clue: it still cd's into these! my $got = $File::Find::name; ( -f $got ) or return; my @stat = stat($got) or return; ( $stat[9] > $times[0] ) or return; ( $stat[9] < $times[1] ) or return; print "$got\n"; # if it cleared all the checks print it }
Update: Unfortunately, having used grandfather's point to solve the dichotomy, the no_chdir option was insufficient to prevent it from issuing error messages for protected directories, so as yet I see no alternative but to abandon Find and write my own Traverse routine that tests the protection before trying to recurse into any given directory - if I use glob, I only expect to be adding a couple of lines compared to the find version.
One world, one people
|
|---|