Comparing Directories and Sub-Directories

Posted on 2011-05-02
Last Modified: 2012-06-27

Can anyone help me with a code snippet to compare directories and sub-directories between source  and destination and if something is missing in destination print that Directory and sub directory to a log file.

log file:

Directory |  Sub directory.

The comparison should happen only to a depth level of 2 because there are some other folders under the sub directory which are unnecessary.
Question by:new_perl_user
    LVL 26

    Expert Comment

    This should do it...
    use strict;
    use warnings;
    my $src_dir = '/path/to/source';
    my $dest_dir = '/path/to/dest';
    my $log = '/path/log_file.txt';
    my (%src, %dest);
    do_dir($src_dir, \%src);
    do_dir($dest_dir, \%dest);
    open LOG, '>', $log or die "could not write $log: $!";
    foreach my $dir (keys %src) {
        if (not exists $dest{$dir}) {
            print LOG "$dir |\n";
        foreach my $sub (keys %{$src{$dir}}) {
            print LOG "$dir | $sub\n" unless exists($dest{$dir}{$sub});
    sub do_dir {
        my ($root, $hash) = @_;
        opendir DIR, $root or die "could not open $root: $!";
        foreach my $dir (grep { -d $_ } readdir DIR) {
            do_sub($root, $dir, $hash);
        closedir DIR;
    sub do_sub {
        my ($root, $dir, $hash) = @_;
        opendir SUB, "$root/$dir" or die "could not open $root/$dir: $!";
        foreach my $sub (grep { -d $_ } readdir DIR) {
        closedir SUB;

    Open in new window


    Author Comment

     Tried the above code but it is not printing any data to the log file.
    LVL 26

    Expert Comment

    So what structure is in "source" but not in "dest" that should be printing?

    Author Comment


    The structure I am trying to compare is

    Source; /usr/files/ BATCH1/254234Q

    Destination:  /usr/logs/BATCH1/254234Q

    So from the above example in the destination " /usr/files/BATCH1/254678Q' is missing so it should that print that to log file

    BATCH1 | 254678Q

    These sub directories (254678Q) contains many sub folders again but no need to compare on them .
    Overall comparison should be only on  two levels.

    LVL 4

    Accepted Solution

    Solution without script using rsync dry run

    rsync -nva src dst | tail --lines=+2 | head --lines=-3 | grep -v '/.*/.*/'  >> logfile.txt

    tail and head needed to strip rsync stats (depends on rsync version, maybe not needed)
    grep is stripping everything from result that has 3 or more slashes).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now