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.

  1. use strict;
  2. use warnings;
  3. use File::Find qw(finddepth);
  4. use 5.10.0;
  5.  
  6. my @files;
  7. finddepth(
  8.     sub {
  9.         return if ($_ eq ‘.’ || $_ eq ‘..’);
  10.         push @files, $File::Find::name;
  11.     },
  12.     ‘/my/dir/to/search’
  13. );
  14.  
  15. say $_ for @files;
DiggEmailRedditShare

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

2 Comments »

 
  • Just in case that you wanted to try something else I would recommend that you tried some of the other File::Find::* modules at CPAN.

    Htbaa Reply:

    Ah thanks for the tip. I wasn’t aware of these.

 
Get Adobe Flash playerPlugin by wpburn.com wordpress themes