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

I'm wanting to parse a certain directory tree and find duplicate directory names contained within. Files contained within are of no concern. I've looked at the File::Find::Duplicate module but am not sure if it can be modified to do this. Does anyone know of a module that can?

Replies are listed 'Best First'.
Re: Duplicate Directory Names
by sgifford (Prior) on Oct 17, 2003 at 21:03 UTC
    File::Find::Duplicate seems to look for files with identical contents. If you just care about identical names, you can use File::Find:
    #!/usr/bin/perl -w use strict; use File::Find; find(\&wanted,'.'); use vars qw(%dirseen); sub wanted { return unless -d $_; if ($dirseen{$_}) { print "Duplicate directory name $_ in $File::Find::dir\n"; } $dirseen{$_}++; }
      Thanks for this information. It saved a big headache. Now I've got to go in and rename the duplicates with a one-up sequence number appended to the directory name. I'm hoping that 'rename' will work on directories.
        rename will work fine on directories as long as they're on the same filesystem.
Re: Duplicate Directory Names
by Zaxo (Archbishop) on Oct 17, 2003 at 21:16 UTC

    File::Find is good for this sort of thing,

    use File::Find; my @dupes; { my %dirnames; sub dupe_dir { -d or return; if (exists $dirnames{$_}) { push @dupes, $File::Find::name; } else { ++$dirnames{$_} } 1; } } find(\&dupe_dir) print $_, $/ for @dupes;
    That searches everything under the current directory for duplicate directory names. The search is breadth first. Caution, untested.

    After Compline,
    Zaxo

Re: Duplicate Directory Names
by BazB (Priest) on Oct 17, 2003 at 20:57 UTC

    Are you sure they're duplicates?
    Unless I'm very much mistaken, (certainly on UNIX) you cannot have files/directories/special files with the same name in a given directory.

    Update:
    Doh! Across an entire directory tree it's possible (obviously) to have duplicated names.
    You should probably look at $File::Find::dir and $File::Find::name from File::Find's wanted() method to distinguish between a non-uniquely named files/directories by location.

    Update 2: Use a hash.


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

      This can be handled neatly with File::Find::Rule as well, which is a nicer interface to File::Find:

      #!/usr/bin/perl -l use File::Find::Rule; print foreach File::Find::Rule ->directory ->exec(sub { $seen{$_}++ }) ->in(shift || ".");

      Tony