Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I loop through files in a directory in Perl? [duplicate]

Tags:

file

perl

Possible Duplicate:
How can I list all of the files in a directory with Perl?

I want to loop through a few hundred files that are all contained in the same directory. How would I do this in Perl?

like image 707
adhanlon Avatar asked Jan 27 '10 18:01

adhanlon


People also ask

How do I loop through all files in Perl?

You can use readdir or glob. Or, Path::Tiny: @paths = path("/tmp")->children; @paths = path("/tmp")->children( qr/\. txt$/ );

How do I get a list of files in a directory in Perl?

If you want to get content of given directory, and only it (i.e. no subdirectories), the best way is to use opendir/readdir/closedir: opendir my $dir, "/some/path" or die "Cannot open directory: $!"; my @files = readdir $dir; closedir $dir; You can also use: my @files = glob( $dir .

How do I search for a file in Perl?

Find modules in Perl has all the functions similar to the Unix Find command. Find function takes two arguments: 1st argument is a subroutine called for each file which we found through find function. 2nd argument is the list of the directories where find function is going to search the files.


1 Answers

#!/usr/bin/perl -w  my @files = <*>; foreach my $file (@files) {   print $file . "\n"; } 

Where

 @files = <*>; 

can be

 @files = </var/www/htdocs/*>;  @files = </var/www/htdocs/*.html>; 

etc.

like image 60
EToreo Avatar answered Oct 01 '22 03:10

EToreo