#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $path = shift || '.'; print Dumper traverse($path); sub traverse { my ($path) = @_; my @files = `find $path -name '*.c' -o -name '*.txt'`; return if not -d $path; opendir my $dh, $path or die; while (my $sub = readdir $dh) { next if $sub eq '.' or $sub eq '..'; traverse("$path/$sub"); } close $dh; chomp @files; return \@files; } __DATA__ $ perl file.pl $VAR1 = [ './counts.txt', './file.txt', './sample.c', './testDir/anotherSample.c', './test.txt' ];