[TAG] 2-cent Tip: Perl Search Directory Function

Ben Okopnik ben at linuxgazette.net
Thu Jun 5 07:28:57 MSD 2008


Jan Engelhardt <jengelh at medozas.de> wrote:
> On Wed, Jun 04, 2008 at 09:32:20PM -0400, Benjamin Okopnik wrote:
> 
> >Why not just use 'File::Find'? It's included in the default Perl 
> >install, and is both powerful and flexible. 
> 
> Why not just use sh?
> 
> 	find . -type d -print0 | xargs -0r do_whatever_you_want.sh
> 
> (resp. xargs -0r/-0rn1 perl -e 'do whatever you want here')

Because File::Find can do everything that 'find' can - and do it faster
and with a lot more flexibility (try asking 'find' to give you just the
current filename - or just the name of the directory that it's
traversing.) Because File::Find can be used on multiple platforms -
including Solaris and MacOS, where 'find' is Stone-Age primitive.  Also,
because Perl is *much* smarter about regexes (yes, I know about the
'-regextype' option; even the 'posix-extended' argument isn't anywhere
nearly as smart as Perl's regexen) - and because Thomas wanted to search
multiple directories, which 'find' doesn't do, TTBOMK.

Oh, and because it doesn't need 'xargs' or any equivalent of it - since
the files (or directories) are processed one at a time rather than being
spit out as a (possibly) huge list.

That's just a few reasons, off the top of my head. :)


-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *




More information about the TAG mailing list