Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: File::Find question

by pfaut (Priest)
on Feb 23, 2003 at 05:29 UTC ( #237860=note: print w/ replies, xml ) Need Help??


in reply to File::Find question

File::Find is a perl implementation of the unix find utility. It is useful for cases when you need to find files to process with perl.

Since what you want to do is delete files which you can do with the unix rm utility, why not just use find and rm? To delete all files named *.b from under /path/to/directory, use this:

find /path/to/directory -name '*.b' -exec rm {} \;

If you need to delete these files as part of processing being done by an existing perl script, then File::Find is the answer. Something like this (untested):

use File::Find; find(sub { $_ =~ /\.b\z/ && unlink $_ }, '/path/to/directory' );
--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';


Comment on Re: File::Find question
Select or Download Code
Re: Re: File::Find question
by coolmichael (Deacon) on Feb 23, 2003 at 07:30 UTC
    One of my first memories with Perl is reading about find2perl. I had no idea that you could translate shell to perl so easily. Anyway, using find2perl is very easy and demonstrates how to use File::Find.
    [michael]$ find2perl /path/to/directory -name '*.b' -exec rm {} \;
    (mostly copied and pasted into the shell) which prints a complete File::Find based Perl script ready to run. It's a little verbose, so I've removed some of the less important lines for the sake of clarity.
    #! /usr/bin/perl -w use strict; use File::Find (); sub wanted { /^.*\.b\z/s && (unlink($_) || warn "$name: $!\n"); } # Traverse desired filesystems File::Find::find({wanted => \&wanted}, '/path/to/directory'); exit;
    --
    negativespace.net - all things inbetween.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://237860]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-09-22 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (182 votes), past polls