code_junkie

How do I get a directory listing in Perl?

Nov 11th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. opendir my($dh), $dirname or die "Couldn't open dir '$dirname': $!";
  2. my @files = readdir $dh;
  3. closedir $dh;
  4. #print files...
  5.  
  6. $listing = `ls`;
  7.  
  8. File::Find::Rule->maxdepth(1)->directory->in($base_dir);
  9.  
  10. #!/usr/bin/perl
  11.  
  12. use strict;
  13. use warnings;
  14.  
  15. # See http://search.cpan.org/~rclamp/File-Find-Rule-0.32/README
  16. use File::Find::Rule;
  17.  
  18. # If a base directory was not past to the script, assume current working director
  19. my $base_dir = shift // '.';
  20. my $find_rule = File::Find::Rule->new;
  21.  
  22. # Do not descend past the first level
  23. $find_rule->maxdepth(1);
  24.  
  25. # Only return directories
  26. $find_rule->directory;
  27.  
  28. # Apply the rule and retrieve the subdirectories
  29. my @sub_dirs = $find_rule->in($base_dir);
  30.  
  31. # Print out the name of each directory on its own line
  32. print join("n", @sub_dirs);
  33.  
  34. chdir $dir or die "Cannot chroot to $dir: $!n";
  35. my @files = glob("*.txt");
  36.  
  37. my @files = map { chomp; $_ } `find`;
  38.  
  39. my $dir = </dir/path/*>
Add Comment
Please, Sign In to add comment