#!usr/bin/perl # ADD THESE TWO LINES! use strict; use warnings; # so you are forced to declare variables with my my $path='/home/something/something/something'; opendir(DIR,$path) or die"couldnt open the directory\n"; # parens not needed # UPDATE: assignement to $_ is needed with older perls: nowadays while(readdir DIR) will suffice # as choroba spotted it is since 5.12 that readdir automatically feeds $_ # doc says: As of Perl 5.12 you can use a bare readdir in a while loop, which will set $_ on every iteration. while($_ = readdir DIR) { # this is wrong! # if(-d BUS) if ($_ eq 'BUS' and -d $path.'/'.$_) #corrected by Corion { # append BUS to the path # infact you are not changing directory my $buspath = $path.'/'.$_; opendir(VEH, $buspath) or die"couldnt open the directory [$buspath]\n"; while($_ = readdir VEH) { print "$_\n" if -f $buspath.'/'.$_; } } }