iterate through all the files in a directory
December 12th, 2006 by Lawrence David
glob is perl’s version of “ls”: it’ll list all of the files in a directory that match the criteria you set forth. i’ve used it here to help me iterate through all of the files in a directory that end in “.phy”. i specify this directory as a command line argument [$ARGV[0]].
#!/usr/bin/perl
use warnings;
# iterate through all sequence files in this directory@phy_files = glob “$ARGV[0]*.phy”;
foreach $phy_file (@phy_files) {
print $phy_file . “\n”;
}
this code can be saved as dir_list.pl and called in the following manner:
$dir_list.pl /my_dir/to_search/
Can be written much, much shorter:
perl -le "print for glob $ARGV[0].'*.pyh'"
Hi! I was surfing and found your blog post… nice! I love your blog. Cheers! Sandra. R.