a Pearl script that reads through all sub directories (511 Views)
Reply
Regular Advisor
nipun_2
Posts: 232
Registered: ‎02-09-2005
Message 1 of 4 (511 Views)
Accepted Solution

a Pearl script that reads through all sub directories

Hi,
I have not scripted in Pearl before. I am aware that Pearl is a great tool for dealing with files.

I need to get the list of all the files (not directories) in the current directory as well as the sub directories.

Is there a pearl script/command readily available to do this sort of thing.

what I am looking for is

execute

pearl_script

output will be a text file containing all the files in the 'Directory Name' as well as all it's sub directories.

I have NEVER touched Pearl so if you have command that does that with correct parameters. Please provide some details so that I can use it.

Any information you can provide would be helpful.

Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 2 of 4 (511 Views)

Re: a Pearl script that reads through all sub directories

Perl, not pearl.

Why perl?

find -type f

or, perhaps:

( cd ; find . -type f)

depending on exactly what you'd like to see.

> output will be a text file

Redirect stdout to a file.

find -type f > out_file
Regular Advisor
nipun_2
Posts: 232
Registered: ‎02-09-2005
Message 3 of 4 (511 Views)

Re: a Pearl script that reads through all sub directories

Hi Steven,
Thanks for the response and the correction!

The reason for Peal is because I need to use it as a script for Matlab Program. This way I am O.S independent as Matlab can run Perl Scripts internally on Linux, Unix, Windows Platform.

Nipun
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 4 of 4 (511 Views)

Re: a Pearl script that reads through all sub directories

Hi there,

I hear you on the multiplatform thing.
I have a collection of perl scripts to help with SAP benchmarks and the exact same scripts all work on HPUX, Tru64, Linux, Windoze.

Now with perl there is always more than one way to do something.
For your question you want to read up on
- glob
- readdir
- module: File::Find;

------------------ partial find example ---
# loop through arguments which are all the tops of directory trees, looking for non-zero-length files called .err or .elg

use warnings;
use strict;
use File::Find;

my ($arg, $i);
my (%found);
while ( $arg = shift @ARGV ) {
print ++$i, " $arg\n";
chomp $arg;
foreach (glob ($arg)) {
print ++$i, " - \'$_\'\n";
find(\&wanted, $_) if -d;
}
}

sub wanted {
my ($type, $size, $file, $spec);
if (/\.(err|elg)/i && -f $_ && ($size=-s)) {
$type = $1;
$spec = $File::Find::name;
$file = $_;
:



-------------------- READDIR --------
:
opendir(DIR,$source) || &error(__LINE__,"opendir");
@entries = grep(!/^\.\.?$/,readdir(DIR));
closedir(DIR) || &error(__LINE__,"closedir");
:


------------------ simple glob --------

while ($files = shift @ARGV) {
while ($file = glob ($files)) {
:


---------------- simpler glob -----

perl -le 'print while (<*.csv>)'

Hein.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.