Skip to content

Instantly share code, notes, and snippets.

@davetang
Last active December 28, 2015 09:29
Show Gist options
  • Save davetang/7479769 to your computer and use it in GitHub Desktop.
Save davetang/7479769 to your computer and use it in GitHub Desktop.
Perl script that takes two directory paths, one old and one new, compares the two and copies directories in the old to the new if it doesn't exist.
#!/bin/env perl
use strict;
use warnings;
my $usage = "Usage: $0 <old_dir> <new_dir>\n";
my $old = shift or die $usage;
my $new = shift or die $usage;
my %current = ();
opendir(NEW,$new) || die "Could not open directory $new: $!\n";
while(my $infile = readdir(NEW)){
next if $infile =~ /^\./;
next unless -d "$new/$infile";
$current{$infile} = 1;
}
closedir(NEW);
opendir(OLD,$old) || die "Could not open directory $old: $!\n";
while(my $infile = readdir(OLD)){
next if $infile =~ /^\./;
next unless -d "$old/$infile";
next if exists $current{$infile};
my $command = "cp -r $old/$infile $new";
warn("Running $command\n");
system($command);
}
closedir(OLD);
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment