Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Is there a unix find equivalent that can be processed inside a perl script?

by behindalens (Initiate)
on Sep 18, 2008 at 22:34 UTC ( [id://712389]=perlquestion: print w/replies, xml ) Need Help??

behindalens has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,
I appreciate any help or guidance to this question as I am a novice to programming or anything resembling cs. I need to fill an array with the file paths of files, lets say its .txt files in this case. In a shell I would use the command:
find . -name "*.txt"

Can I run this within a perl script so I can fill an array with these filepaths (to be used in my script).
I appreciate the help!

Replies are listed 'Best First'.
Re: Is there a unix find equivalent that can be processed inside a perl script?
by broomduster (Priest) on Sep 18, 2008 at 22:39 UTC
    See File::Find, which is part of the Perl distribution. There's also find2perl, which shows how a particular find command can be converted into Perl code that uses File::Find.
Re: Is there a unix find equivalent that can be processed inside a perl script?
by kyle (Abbot) on Sep 18, 2008 at 22:39 UTC
Re: Is there a unix find equivalent that can be processed inside a perl script?
by merlyn (Sage) on Sep 19, 2008 at 01:52 UTC
      What, no magazine article on the subject? ;-)

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Is there a unix find equivalent that can be processed inside a perl script?
by jwkrahn (Abbot) on Sep 19, 2008 at 00:52 UTC
    chomp( my @filepaths = qx[find . -name "*.txt"] );
Re: Is there a unix find equivalent that can be processed inside a perl script?
by repellent (Priest) on Sep 19, 2008 at 05:22 UTC
    use File::Find; my @filepaths; find sub { push @filepaths, $File::Find::name if /\.txt$/ }, '.'; # $_ is set to the current file basename

    Update: Made a fix. Thanks, ikegami.
      Not quite.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://712389]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-28 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found