Perl - Read folders recursively

Posted on 2011-05-10
Last Modified: 2012-05-11
I would like to build code that runs on the file system (from a specific folders onwards) and retrieve all files that do not have extension of *.atr. What is the best method to do that? Should i use glob ?
Thank you
Question by:tamirmilo
    LVL 26

    Accepted Solution

    This should do what you want...
    use strict;
    use warnings;
    my $root = shift or die "Usage: $0 root_folder\n";
    my $rx = shift || '\.atr$';
    my @no_match = process($root);
    print "files that did not match $rx:\n", join("\n", @no_match), "\n";
    sub process {
        my ($dir) = @_;
        my (@dirs, @bad);
        opendir DIR, $dir or die "could not open dir $dir: $!";
        while (my $fil = readdir DIR) {
            if (-d "$dir/$fil" and $fil !~ m{^\.+$}) {
                push @dirs, "$dir/$fil";
            } elsif (-f "$dir/$fil" and $fil !~ m{$rx}) {
                push @bad, "$dir/$fil";
        closedir DIR;
        foreach my $d (@dirs) {
            push @bad, process($d);
        return @bad;

    Open in new window

    LVL 13

    Assisted Solution

    by:Carl Bohman
    It looks like wilcoxon's solution should work.  In general, though, maintenance is easier if you leverage standard modules (such as File::Find) when it makes sense.  I'll use wilcoxon's same command line interface for consistency.

    use strict;
    use warnings;
    my $root = shift or die "Usage: $0 root_folder\n";
    my $rx = shift || '\.atr$';
    # This pre-compiles the regex so that it doesn't need to be compiled each time it is used.
    my $crx = qr/$rx/ or die "Unable to compile regex: $rx\n";
    my @no_match;
    sub wanted
       push @no_match, $File::Find::name if (-f _ && ! /$regex/)
    find(\&wanted, $root);
    print "files that did not match $rx:\n", join("\n", @no_match), "\n";

    Open in new window


    Author Closing Comment

    Thank you both.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now