比较两个文件夹内容的所有者和权限?

J.O*_*sen 10 linux files

如何比较两个文件夹内容的所有者和权限?是否有类似diff命令可以递归比较两个文件夹并显示所有者和权限差异?

cjc*_*cjc 11

与所有事情一样,解决方案是一个 perl 脚本:

#!/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 );
}
Run Code Online (Sandbox Code Playgroud)

查看http://perldoc.perl.org/functions/stat.htmlhttp://perldoc.perl.org/File/Find.html了解更多信息,特别是stat如果您想比较其他文件属性的信息。

如果目录 2 中不存在文件,但目录 1 中存在文件,也会有输出,因为它们stat会有所不同。


小智 7

查找并统计:

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

在两个目录中运行它,然后比较两个列表文件。

将您从 Perl 的邪恶中拯救出来...

  • 然后只需比较结果即可:) (2认同)