Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Perl, push into an array a list of files in a directory except for a few select files based on file name
    primarykey
    data
    text
    <p>I'm uploading a tarball through a webpage, dropping it in to /tmp/ then asking this script (which will be invoked via crontab) to:</p> <p>1.) extract it</p> <p>2.) build a list of all of the files (only files and recursively) in the directory</p> <p>3.) search each file for a string and print that filename and line with matched string to a file.</p> <p>Everything is working up to the part where I want to build a list of files in the (extracted tarball) directory. If I <em>don't</em> put a "!" in front of the regex on line 6 in my code (matching only files that are .bak, .conf, .cfg), then I only get a dozen files in @filelist (as I'd expect, printed by the code on line 13). </p> <p>However, if I put a "!" in front of my regex on line 6 (intended to match everything <em>but</em> those files), line 13 will print all filenames, including files with .bak, .conf, and .cfg extensions.</p> <p>How can I get a collection of filenames in the (extracted tarball) directory except for those that I'm just not concerned about?</p> <p>This is my code, roughly (stripped down, untested.) I'm a perl newb so I apologize for the ugliness of what I have here but it is what it is.</p> <pre><code> 1 sub loadFiles { 2 my $dir=shift; 3 find(\&amp;recurDir,"$dir"); 4 } 5 sub recurDir { 6 if ( $File::Find::name =~ /(\.bak|\.conf|\.cfg)$/i ) { 7 push @filelist, $File::Find::name; 8 } 9 print "$File::Find::name\n"; 10 } 11 sub searcher { 12 my $file=$_; 13 print "Searching $file\n"; 14 } 15 my $tarball = '/tmp/mytarball.tar.gz'; 16 my $ae = Archive::Extract-&gt;new( archive=&gt;$tarball ) || die ("$!"); 17 $ae-&gt;extract( to=&gt;$UPLOAD_DIR ) || die ("$ae-&gt;error"); 18 my $dir_loc = File::Spec-&gt;catfile( $UPLOAD_DIR, $ae-&gt;files-&gt;[0]); 19 loadFiles("$dir_loc"); 20 find(\&amp;searcher, @filelist); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload