Use File::Find for recursive directory searches

One of the finest Perl (core!) module I’ve been using lately is File::Find. With File::Find it’s possible to traverse through a directory recursively to find anything you want from it. It comes with a number of options to alter the search behavior and a custom callback has to be supplied to determine what to do with the found file. Such as adding it to a list if it meets certain criteria, computing MD5 checksums or whatever else floats your boat.

Here’s a small example I also posted some time ago on Stack Overflow.

use strict;
use warnings;
use File::Find qw(finddepth);
use 5.10.0;

my @files;
finddepth(
    sub {
        return if ($_ eq '.' || $_ eq '..');
        push @files, $File::Find::name;
    },
    '/my/dir/to/search'
);

say $_ for @files;

2 thoughts on “Use File::Find for recursive directory searches”

Comments are closed.

Scroll to Top