Comparing Directories and Sub-Directories

Posted on 2011-05-02
Medium Priority
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
  • 2
  • 2
LVL 27

Expert Comment

ID: 35505116
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

ID: 35505227
 Tried the above code but it is not printing any data to the log file.
LVL 27

Expert Comment

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

Author Comment

ID: 35505832

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.


Accepted Solution

a1j earned 2000 total points
ID: 35507502
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).

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans
Suggested Courses

864 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