sub checkpermissions { my( $directory ) = @_; my ( $targetdir = $directory ) =~ s!^$sourcedir!$targetdir!; # now we check first of the item in $targetdir exists : if (-f $targetdir || -d $targetdir) { my ($perm_s, $perm_t); my @statresults; # This could be more optimized, but ... @statresults = stat( $directory ); $perm_s = @statresults[2]; @statresults = stat( $targetdir ); $perm_t = @statresults[2]; if ($perm_s != $perm_t ) { print "$targetdir has wrong permissions."; chmod $perm_s, $targetdir or die "Couldn't update the permissions for $targetdir : $!\n"; }; } else { # I assume you don't have links or other stuff. # If you have symlinks etc., use rsync or tar # I cheat and call cp for copying all stuff recursively system "cp", "-R", "$directory", "$targetdir"; }; };