Find files with given attributes

Posted on 2007-10-09
Last Modified: 2010-03-05
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.
Question by:Belazir
    LVL 5

    Accepted 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


    Author Comment

    You know I said I'm a beginner?

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

    Thanks  : )
    LVL 5

    Expert Comment


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

    my @directories_to_search = ('C:/home', 'C:/projects','F:/gudP0n');

    Author Comment

    Okay, I'll give this a shot on Friday and see how I get on.  thanks

    Author Comment

    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?
    LVL 84

    Expert Comment

    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.
    LVL 84

    Expert Comment

           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

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    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…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    734 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

    18 Experts available now in Live!

    Get 1:1 Help Now