• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

Find files with given attributes

I'm looking for a little bit of code that brings back files having certain attributes, for example file suffix and last modified date, in named folders and subfolders.  So, for example, I'd like to be able to find files ending .tmp in the root of C: with a last modified date of more than a week before today.  (In this case I then want to delete them.)

I know this is pretty simple code but I'm a Perl beginner so a full code example would be appreciated please.
  • 3
  • 2
  • 2
1 Solution
I'd recommend using the "File::Find" module.


Using this with find2perl, you can execute a command of the form:

find2perl / -name .tmp\* -mtime +7 \
        -exec rm -f {} \;

You would have to run this in every directory you want this to run.

Using just the find module:

use File::Find;
    find(\&wanted, @directories_to_search);
    sub wanted {
        if(/.*\.tmp/ &&  # File ends with ".tmp"
        int(-M _) > 7){     #File's been modified in the past seven days
               unlink($_);    #delete the file

BelazirAuthor Commented:
You know I said I'm a beginner?

Could you let me know how to populate the directories_to_search array?

Thanks  : )

I was wondering if you'd need help with that detail.

my @directories_to_search = ('C:/home', 'C:/projects','F:/gudP0n');
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

BelazirAuthor Commented:
Okay, I'll give this a shot on Friday and see how I get on.  thanks
BelazirAuthor Commented:
Couple of questions:

The wanted sub is what's called by the first parameter of the Find command?  How does this sub know the filename passed?  There doesn't appear to be any parameters.
Will this search subfolders?  If not, how might I get it to iterate through subfolders?
perldoc File::Find

       The wanted function

       The "wanted()" function does whatever verifications you want on each
       file and directory.  Note that despite its name, the "wanted()" func-
       tion is a generic callback function, and does not tell File::Find if a
       file is "wanted" or not.  In fact, its return value is ignored.

       The wanted function takes no arguments but rather does its work through
       a collection of variables.

       $File::Find::dir is the current directory name,
       $_ is the current filename within that directory
       $File::Find::name is the complete pathname to the file.
       These are functions for searching through directory trees doing work on
       each file found similar to the Unix find command.  File::Find exports
       two functions, "find" and "finddepth".  They work similarly but have
       subtle differences.

             find(\&wanted,  @directories);
             find(\%options, @directories);

           "find()" does a depth-first search over the given @directories in
           the order they are given.  For each file or directory found, it
           calls the &wanted subroutine.  (See below for details on how to use
           the &wanted function).  Additionally, for each directory found, it
           will "chdir()" into that directory and continue the search, invok-
           ing the &wanted function on each file or subdirectory in the direc-

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now