Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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';

Replies are listed 'Best First'.
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;
    -- - all things inbetween.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://237860]
[erix]: those clever germans

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-08-17 14:35 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (288 votes). Check out past polls.