Skip to content

Instantly share code, notes, and snippets.

@takus
Created October 9, 2011 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takus/1273629 to your computer and use it in GitHub Desktop.
Save takus/1273629 to your computer and use it in GitHub Desktop.
Find all files under a base directory recursively
#!/usr/bin/env perl
my $base_dir = shift;
my $ref_files = &get_file_paths($base_dir);
for (@$ref_files) {
print "$_\n";
}
sub get_file_paths {
my $base_dir = shift;
my @files;
opendir( DIR, $base_dir ) or die "can't open $base_dir";
my @temp = readdir(DIR);
closedir(DIR);
foreach my $path ( sort @temp ) {
next if ( $path =~ /^\.{1,2}$/ );
my $full_path = "$base_dir/$path";
# find file paths recursively
if ( -d $full_path ) {
my $ref_files = &get_file_paths($full_path);
if ( @$ref_files > 0 ) {
push( @files, @$ref_files );
}
}
# add file path
else {
push( @files, $full_path );
}
}
return \@files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment