#!/usr/bin/env perl use warnings; use strict; my @files = glob '/path/*.log'; my $out_filename = 'output.txt'; open my $out_fh, '>', $out_filename or die "open $out_filename: $!"; for my $filename (@files) { next if $filename =~ /regex to exclude undesired filenames/; open my $fh, '<', $filename or die "open $filename: $!"; while (<$fh>) { next unless /regex to match desired lines/; print {$out_fh} $_; } close $fh; } close $out_fh; #### $ ls /path/*.log | grep -v filename_exclude_pat | xargs grep -h line_include_pat >output.txt