Directory search

E

eyal.susser

Hi,

I'm working on a Fedora Core 3 machine. I am also a perl newbie. I
would like to write a script that iterates through directories and
finds files with specific attributes.
I wrote a subroutine called scan:

sub scan
{
$dirname = shift;
open(DIRAHNDLE , $filename);
@contents = <$dirname . "/*">
close(DIRHANDLE);

foreach $member (@contents)
{
if(-d $member)
print("Dir: " . $dirname . "/" . $member);

else
print("File: " . $member);
}
}

This was just for testing. I ran it on /home/username, and have
several problems:
1. I get a result like /home/username/home/username, probably because
of the . directory
2. I get a list of all the files and directories under / ! Why is
that?
3. So how do I write a script that does what I want? I wanted to use
recursion, but with these wacky results, it would lead to an infinite
loop!
 
T

Tintin

Hi,

I'm working on a Fedora Core 3 machine. I am also a perl newbie. I
would like to write a script that iterates through directories and
finds files with specific attributes.
I wrote a subroutine called scan:

sub scan
{
$dirname = shift;
open(DIRAHNDLE , $filename);
@contents = <$dirname . "/*">
close(DIRHANDLE);

foreach $member (@contents)
{
if(-d $member)
print("Dir: " . $dirname . "/" . $member);

else
print("File: " . $member);
}
}

This was just for testing. I ran it on /home/username, and have
several problems:
1. I get a result like /home/username/home/username, probably because
of the . directory
2. I get a list of all the files and directories under / ! Why is
that?
3. So how do I write a script that does what I want? I wanted to use
recursion, but with these wacky results, it would lead to an infinite
loop!

perldoc File::Find

or write it as a shell script using the find(1) command.
 
T

Tore Aursand

I'm working on a Fedora Core 3 machine. I am also a perl newbie. I
would like to write a script that iterates through directories and
finds files with specific attributes.

Use the excellent 'File::Find::Rule' module from CPAN.
 
J

John W. Krahn

I'm working on a Fedora Core 3 machine. I am also a perl newbie. I
would like to write a script that iterates through directories and
finds files with specific attributes.
I wrote a subroutine called scan:

sub scan
{
$dirname = shift;
open(DIRAHNDLE , $filename);

You have a typo, DIRAHNDLE should be DIRHANDLE and besides, open() uses
filehandles not directory handles. You open the file in $filename but you
don't do anything with it, which is OK in this case because you don't test
whether the filehandle is valid or not.

perldoc -f open
perldoc -f opendir

@contents = <$dirname . "/*">

You are using a file glob to match the contents of $dirname followed by the
file '.' followed by all the files in the root directory?

perldoc -f glob
perldoc File::Glob

close(DIRHANDLE);

foreach $member (@contents)
{
if(-d $member)
print("Dir: " . $dirname . "/" . $member);

Since the contents of $dirname are included in $member you are printing it
twice.

else
print("File: " . $member);
}
}

This was just for testing. I ran it on /home/username, and have
several problems:
1. I get a result like /home/username/home/username, probably because
of the . directory
2. I get a list of all the files and directories under / ! Why is
that?
3. So how do I write a script that does what I want? I wanted to use
recursion, but with these wacky results, it would lead to an infinite
loop!

perldoc File::Find



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top