use strict; use warnings; use File::Find::Rule; use Path::Tiny; my @dirs; # populate with list of directories to search my $rule = File::Find::Rule->new; $rule->file; # find only files $rule->name(qr/\.txt$/i); # find (case-insensitive) filenames ending with .txt $rule->extras({no_chdir => 1}); # don't change directories during search my @files = $rule->in(@dirs); foreach my $file (@files) { my $source_dir = path($file)->parent->stringify; my $target_dir; # set this to path of target directory my $outfile; # set to name of file to copy/move out of source directory my $source_file = path($source_dir,$outfile); my $target_file = path($target_dir,$outfile); # to copy the file, uncomment next line #path($source_file)->copy($target_file); # to move the file, uncomment next line #path($source_file)->move($target_file); }