Search code examples
perl

Loop Find Command's Output


I'm wanting to issue the find command in Perl and loop through the resulting file paths. I'm trying it like so (but not having any luck):

my $cmd;

open($cmd, '-|', 'find $input_dir -name "*.fastq.gz" -print') or die $!;

while ($line = <$cmd>) {
   print $line;
}

close $cmd;

Any ideas?

Thanks


Solution

  • You're not applying enough escaping to the * character. Prepending a \ should fix it.

    It's better not to invoke the shell in the first place, by separating the arguments:

    use warnings;
    use strict;
    
    open(my $cmd, '-|', 'find', $input_dir, '-name', '*.fastq.gz', '-print') or die $!;
    
    while (my $line = <$cmd>) {
       print $line;
    }
    
    close $cmd;