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

Hi All, I need a help here, I have a directory which is having files in thousands. My requirement is that in one read I need to read limited number of files from that directory not the all which, I have in that directory.

For example:
I have 20,0000 files in a directory called '/willows/dev/shree/'. I need to read only 500 files in one read. Do we have any Perl utilities to do so.

  • Comment on How to read only limited number of files from a Directory

Replies are listed 'Best First'.
Re: How to read only limited number of files from a Directory
by kcott (Archbishop) on Jun 20, 2013 at 07:34 UTC

    G'day shree,

    See the example code in readdir. Just add a counter and read whatever range you want.

    -- Ken

Re: How to read only limited number of files from a Directory
by hdb (Monsignor) on Jun 20, 2013 at 07:25 UTC
Re: How to read only limited number of files from a Directory
by BillKSmith (Monsignor) on Jun 20, 2013 at 13:13 UTC
    I recently solved a similar problem by creating a subclass of IO::Dir and overriding the read method.
    Bill
Re: How to read only limited number of files from a Directory
by gurpreetsingh13 (Scribe) on Jun 20, 2013 at 08:02 UTC
    Use your unix utilities alongwith. Here is a simple one-liner if you need to read only .txt files
    perl -e 'foreach(`ls /willows/dev/shree/*.txt|head -500`){print `cat $ +_`;}'>>newfile.txt
    That's just a simple code for concatenation.