Feed on

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]].

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/

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

2 Responses to “iterate through all the files in a directory”

  1. on 10 Oct 2008 at 1:17 am Peteris Krumins

    Can be written much, much shorter:

    perl -le "print for glob $ARGV[0].'*.pyh'"

  2. on 10 Sep 2009 at 4:53 pm sandrar

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

Did I get this wrong? Let me know!

Trackback URI | Comments RSS