Harter, Douglas wrote:
> I want to traverse a directory tree, find all files using a
> specific pattern
> (like *.tmp), and then if the modification age (-M) is over a certain
> limit, delete it.
> 
> File::Find looks like it would be good for that, but the man
> page gives no
> real examples. There is a reference to a pfind script on
> CPAN, but it no
> longer seems to exist.
> 
> Could someone give me a SIMPLE way to use File::Find to do the above?

File::Find is simple. You give it a starting directory and a subroutine. It
calls the subroutine once for each file and directory in the starting
directory and in its descendants. In the subroutine, you need to determine
whether that's a file (or directory) you're interested in, and then take
appropriate action.


   #!/usr/bin/perl
   use File::Find;
   find(sub { -f && /\.tmp$/ && -M > 7 && unlink }, '.');

Here I'm passing an anonymous subroutine reference to find(), and telling it
to start in the current directory. Each time my sub is called, $_ will be
the file or directory name. So I apply several tests:

   -f          is $_ a plain file?
   /\.tmp/     does the name end in ".tmp"
   -M > 7      is it older than 7 days
   unlink      if all above are true, remove the file

That's all there is to it. For testing purposes, I suggest you change
"unlink" to "print" and just print out the file names. If you want to print
the complete name with the directory, print $File::Find::name.

HTH

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to