I'm a bit confused from File::Find
documentation... What is the equivalent to $ find my_dir -maxdepth 2 -name "*.txt"
?
Find modules in Perl has all the functions similar to the Unix Find command. Find function takes two arguments: 1st argument is a subroutine called for each file which we found through find function. 2nd argument is the list of the directories where find function is going to search the files.
Perl has a set of useful file test operators that can be used to see whether a file exists or not. Among them is -e, which checks to see if a file exists.
Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text.
Personally, I prefer File::Find::Rule
as this doesn't need you to create callback routines.
use strict; use Data::Dumper; use File::Find::Rule; my $dir = shift; my $level = shift // 2; my @files = File::Find::Rule->file() ->name("*.txt") ->maxdepth($level) ->in($dir); print Dumper(\@files);
Or alternatively create an iterator:
my $ffr_obj = File::Find::Rule->file() ->name("*.txt") ->maxdepth($level) ->start($dir); while (my $file = $ffr_obj->match()) { print "$file\n" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With