sub changelinks { my ($file) = @_; if (-d $file) { opendir(DIR, $file) || die "$!"; my @filenames = readdir(DIR); foreach my $filename (@filenames) { if (($filename ne "." && $filename ne "..")) { changelinks($file."/".$filename); } } closedir(DIR); } else { if ($file =~ /.shtml/i) { And then whatever method of opening and editing each file goes here } } }