Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How do you list all files under a directory?

by daverave (Scribe)
on Sep 22, 2010 at 17:35 UTC ( [id://861322]=perlquestion: print w/replies, xml ) Need Help??

daverave has asked for the wisdom of the Perl Monks concerning the following question:

I usually use something like
my $dir="/path/to/dir"; opendir(DIR, $dir) or die "can't open $dir: $!"; my @files = readdir DIR; closedir DIR;
or sometimes I use glob, but anyway, I always need to add a line or two to filter out . and .. which is quite annoying.

How do you usually go about this common task?

Replies are listed 'Best First'.
Re: How do you list all files under a directory?
by Corion (Patriarch) on Sep 22, 2010 at 17:48 UTC
Re: How do you list all files under a directory?
by toolic (Bishop) on Sep 22, 2010 at 18:18 UTC
Re: How do you list all files under a directory?
by superfrink (Curate) on Sep 22, 2010 at 18:25 UTC
    Here is some code to find emacs ~ files and delete them.
    #!/usr/bin/perl -w use strict; use File::Finder; use Data::Dumper; # list of dirs to look in my @start_dirs = ( '/home/chad/' , ); my $finder = File::Finder # do not go into the ~chad/Archives/ directory. ->type('d')->name('Archives')->prune->comma # match the following properties. ->type('f') # files ->name('*~') # named glob(*~) ->ctime('+6') # inode changed time more than 6 days ago # ->ls() # print `ls -l` of file ; # run the finder command now. my @file_list = $finder->in(@start_dirs); # delete the found files. my $count = unlink @file_list; print "\nUnlinked $count files.\n"; exit(0);
Re: How do you list all files under a directory?
by perl-diddler (Chaplain) on Sep 22, 2010 at 21:53 UTC
    For looking for all the files with a given extension, I used:
    my @uwavs = grep {/^(.*?$SrcExt)$/ && -f $_ } readdir $dirhandle;

    To exclude dot files, I'd probably use something like /^([^\.]+.*)$/ for my regex. Obviously in my case, I also only wanted files, so you might not need the "-f $_" part.

    As usual, there's always a bunch of ways to do things.

Re: How do you list all files under a directory?
by Proclus (Beadle) on Sep 22, 2010 at 22:03 UTC
    My favorite IO module is IO::All
    #Read all the files/directories in a directory: $io = io('my/directory/'); # Create new directory object @contents = $io->all; # Get all contents of dir
Re: How do you list all files under a directory?
by Marshall (Canon) on Sep 26, 2010 at 11:56 UTC
    This glob stuff can get hairy as there are multiple versions of it. I've had some big troubles in the past with this in the Windows environment. Adding a grep to process the list from readdir is portable and is just a few more characters to your standard "formula".

    Technically a directory is a file, but -f means the subset of files that aren't directories (which excludes '.' and '..'). Be sure to use a full path name in the grep otherwise you will be testing against files in the directory that the program is executing in, which is probably not what you want! When I do want that, I set $dir = "." and use the same formula.

    my $dir="/path/to/dir"; opendir(DIR, $dir) or die "can't open $dir: $!"; my @files = grep {-f "$dir/$_"} readdir DIR; closedir DIR;
    Updated: added single quotes to make '.' and '..' more clear in some fonts.
Re: How do you list all files under a directory?
by dasgar (Priest) on Sep 23, 2010 at 06:07 UTC

    I usually cheat by calling the 'ls' or 'dir' command (depending on OS) with the appropriate options inside of backticks. Since I primarily work in Windows, sometimes I'll bust out the Win32::File module to test the returns of readdir to determine what's a file and what's a directory.

    Instincts tell me that there's probably a 'better' way to do it, but I haven't found it yet and have been too impatient to look for it when I needed to do something like this.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://861322]
Approved by TStanley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2024-04-23 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found