Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Listing Files

by vchavan (Initiate)
on Sep 16, 2011 at 04:46 UTC ( #926308=perlquestion: print w/replies, xml ) Need Help??
vchavan has asked for the wisdom of the Perl Monks concerning the following question:

Hello I want to list all the files from a directory and its sub directories till all the child directories get listed . In another words till there is no subdirectory remained under the directory from where we started. I also want to compare all the files of two similar directories of different versions. Could you please help me ? Thanks All

Replies are listed 'Best First'.
Re: Listing Files
by thewebsi (Scribe) on Sep 16, 2011 at 07:26 UTC

    File::Find is good for listing files, but for comparing 2 directories, I prefer rsync, which can do that in 1 line. There is a Perl wrapper File::Rsync, and a Perl implementation File::RsyncP as well.

Re: Listing Files
by zentara (Archbishop) on Sep 16, 2011 at 10:08 UTC
    Here is how to get a File::Find recursive list
    #!/usr/bin/perl use strict; use File::Find; find (\&wanted, '.'); sub wanted { next if -d; # skip if it's a directory # $_ is the name of the file # $File::Find::name is the full path and name to the file print "$File::Find::name\n"; }
    When you say compare, what do you mean? Do you want to check for exact identity? Same names? Same size? What?

    Here is a way to put the listings into a hash, so you could cycle thru the hash and compare.

    #!/usr/bin/perl -w use strict; use Data::Dumper; sub hashdir { my $dir = shift || '.'; opendir my $dh, $dir or die $!; my $tree = {}->{$dir} = {}; while ( my $file = readdir($dh) ) { next if $file =~ m[^\.{1,2}$]; my $path = $dir . '/' . $file; $tree->{$file} = hashdir($path), next if -d $path; push @{ $tree->{'.'} }, $file; } return $tree; } my $tree = hashdir(shift); print Dumper $tree;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thanks By comparison I want to compare their names. and also check if there is any zero byte file and if there is then list it out. First I need to collect all the files (recursively) from two similar directories say Dir_build1 and Dir_Build2 in say 2 arrays and then compare them by name and find out any differences. Then its directory i.e. if difference comes out in Dir_build2 then it should display that Dir_Build 2 have x y z files different than Dir_build1 Could you please help me ?

        Honestly, I'd do this with a few command-line tools, rather than Perl, but the concepts would be the same:

        find Dir_build1 Dir_build2 -type f -size 0 >/tmp/zeros find Dir_build1 -type f | sort >/tmp/1 find Dir_build2 -type f | sort >/tmp/2 comm -3 /tmp/1 /tmp/2

        To do it in Perl, I'd use File::Find to recursively scan through each directory, adding the file names to an array (probably a hash, actually) for each, then sort and compare the two. During the scans, I'd also build a list of zero-byte files, to avoid scanning through them twice, of course.

        Why don't you post what you have tried? Then we can help you fix it. This is not a code writing service and the posts above should be enough to get you started.

        "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: Listing Files
by repellent (Priest) on Sep 16, 2011 at 05:22 UTC
      "File::Find" module helps to processs all the files and folders in a directory.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://926308]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2017-01-23 22:28 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (198 votes). Check out past polls.