#!/usr/bin/perl -w # Shell commands $find="find"; $rm="rm"; $mkdir="mkdir"; $touch="touch"; sub help { print "$0 \n\n", "Makes old dir match new dir by removing or making or touching\n", "directories and files\n", "Outputs to stdout so pipe it through bash to execute\n\n"; exit; } sub read_entities { my $dir=$_[0]; my $type=$_[1]; my $table=$_[2]; open(F,"$find $dir -type $type |") or die("Can't find $dir: $!\n"); my @names=; close F; for(@names){ chomp; s/^$dir\/{0,1}//; $table->{$_}=1 if length($_); } } # Do command on things in a that aren't in b. a and b are references to hashes. sub process { my $command=$_[0]; my $a=$_[1]; my $b=$_[2]; for(keys %$a){ unless(defined($b->{$_})){ print "$command$_\n"; } } } help unless @ARGV; $olddir=$ARGV[0]; $newdir=$ARGV[1]; # It doesn't matter, but looks neater $olddir=~s/\/*$//; $newdir=~s/\/*$//; # Subdirectories first read_entities($olddir,"d",\%old); read_entities($newdir,"d",\%new); print "# Directories\n"; process("$rm -rf $olddir/",\%old,\%new); process("$mkdir -p $olddir/",\%new,\%old); undef %old; undef %new; # Now files print "# Files\n"; read_entities($olddir,"f",\%old); read_entities($newdir,"f",\%new); process("$rm -f $olddir/",\%old,\%new); process("$touch $olddir/",\%new,\%old);