Grep in sub directories

B

blnukem

Hi All

How do I grep all files with a html extension and html files in sub
directories?

Thanks
Blnukem
 
M

Mothra

blnukem said:
Hi All

How do I grep all files with a html extension and html files in sub
directories?

Thanks
Blnukem
What code have you written so far? Where are you getting stuck?
 
B

blnukem

Mothra said:
What code have you written so far? Where are you getting stuck?


opendir DIR, "../";
@htmlpages = grep /\.html$/i, readdir DIR;

print "@htmlpages";
 
M

Mothra

blnukem said:
opendir DIR, "../";
@htmlpages = grep /\.html$/i, readdir DIR;

print "@htmlpages";
OK, if you're wanting to search recursively, you need to make your code
recursive too. Here is an example (inspired by an example in the book "Perl
for System Administration" from O'Reilly) that keeps track of where you
started and then keeps running the 'scan' sub routine from within itself.
Any files ending in '.html' are pushed into the @htmlpages array.

I recommend that book by the way :)

------------------
#!/usr/bin/perl -w
use strict;
use Cwd;
my @htmlpages;

sub scan{
my $currentdir = shift;
my $startdir = &cwd;

chdir($currentdir);
opendir(DIR, '.');
my @files = readdir(DIR);
closedir(DIR);

foreach (@files){
next if /^\./;
if (-d $_){
&scan($_);
next;
}
if (/\.html$/) {
push (@htmlpages, $_)
}
}
chdir($startdir);
}
&scan('.');
print @htmlpages;
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top