13

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?

6 Answers 6

12

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

#!/usr/bin/perl

use 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.

2
  • If you want the permission printed in UNIX-style, this comes in handy: printf ("Permissions for %s and %s are not the same (%04o != %04o)\n", $file1, $file2, $mode1 &07777, $mode2 &07777) if ( $mode1 != $mode2);
    – Marcus
    Aug 30, 2018 at 12:15
  • Another mod is to have, before the 'my $mode#...' lines if (-e $file2) { and following the print lines, add } else { print "File $file2 does not exist"; }. This is useful for avoiding permissions warnings for non-existent files. May 24, 2022 at 18:17
13

Find and stat:

find . -exec stat --format='%n %A %U %G' {} \; | sort > listing

Run that in both directories then compare the two listing files.

Saves you from the evils of Perl...

1
  • 2
    Then just diff the results :) Nov 1, 2019 at 20:16
1

Do you make sure the 2 folders should be the same recursively to some extent? I think the rsync command is very powerful for that.

In your case you may run:

rsync  -n  -rpgov src_dir dst_dir  
(-n is a must otherwise dst_dir will be modified )

The different files or folders will be listed as the command output.

You can see the man rsync for a more complete explanation of these options.

1
  • using src_dir/ in stead of src_dir in the above command will make its contents just be mapping to the dst_dir's contents )
    – Bill Zhao
    Sep 28, 2017 at 13:16
1

If the two directories have the same structure and you have tree installed, you could diff the directories by doing:

diff <(tree -ap parent_dir_1) <(tree -ap parent_dir_2)
1
  • 2
    Note that this solution will not show or catch changes in file ownership. For a complete solution, use tree -agpu.
    – ATLief
    Jul 3, 2021 at 14:48
0

In bash (or other shells supporting process substition using <(…)) using find … -printf (works using GNU printf):

diff -u \
  <(find path1/ -printf '%P %m\n' | sort) \
  <(find path2/ -printf '%P %m\n' | sort)
-1

ls -al will show the permissions, if both of them are in the same folder you will get something like this:

drwxr-xr-x 4 root  root 4096 nov 28 20:48 temp
drwxr-xr-x 2 lucas 1002 4096 mrt 24 22:33 temp2

The 3rd column is the owner, the 4th is the group.

3
  • Umm, what about the contents of temp and temp2?
    – cjc
    Mar 24, 2012 at 22:16
  • two ways: open 2 shells go into both folders and do the same ls -al command, or 1 shell with tmux or just go into one folder do the command go into the other and do the same command again. Mar 24, 2012 at 22:23
  • 4
    This solution will not scale. Aug 4, 2019 at 8:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .