Mar 24, 2012
tom

Comparing owners and permissions of content of two folders?

Question

How to to compare owners and permissions of content of two folders? Is there something like diff command which compare recursively two folders and display owner and permissions differences?

Asked by RCola

Answer

The solution, as with all things, is a perl script:

#!/usr/bin/perluse File::Find;my $directory1 = '/tmp/temp1';
my $directory2 = '/tmp/temp2';find(\&hashfiles, $directory1);sub hashfiles {
  my $file1 = $File::Find::name;
  (my $file2 = $file1) =~ s/^$directory1/$directory2/;  my $mode1 = (stat($file1))[2] ;
  my $mode2 = (stat($file2))[2] ;  my $uid1 = (stat($file1))[4] ;
  my $uid2 = (stat($file2))[4] ;  print "Permissions for $file1 and $file2 are not the same\n" if ( $mode1 != $mode2 );
  print "Ownership for $file1 and $file2 are not the same\n" if ( $uid1 != $uid2 );
}

Look at http://perldoc.perl.org/functions/stat.html and http://perldoc.perl.org/File/Find.html for more info, particularly the stat one if you want to compare other file attributes.

If files don’t exist in directory2 but exist in directory1, there will also be output because the stat will be different.

Answered by cjc

No related posts.

Leave a comment