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

- 131
- 1
- 2
-
2Then just diff the results :) – Paul Allsopp Nov 01 '19 at 20:16
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.

- 24,916
- 3
- 51
- 70
-
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 '18 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. – michaelkrieger May 24 '22 at 18:17
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.
-
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 '17 at 13:16
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)

- 101
- 2
-
1Note that this solution will not show or catch changes in file ownership. For a complete solution, use `tree -agpu`. – ATLief Jul 03 '21 at 14:48
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)

- 65
- 9
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.

- 16,880
- 9
- 58
- 93
-
-
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. – Lucas Kauffman Mar 24 '12 at 22:23
-
4