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

前端之家收集整理的这篇文章主要介绍了linux – 比较两个文件夹内容的所有者和权限?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何比较两个文件内容的所有者和权限?是否有类似diff命令的东西,它递归地比较两个文件夹并显示所有者和权限差异?

解决方法

与所有事情一样,解决方案是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 );
}

有关详细信息,请查看http://perldoc.perl.org/functions/stat.htmlhttp://perldoc.perl.org/File/Find.html,特别是如果要比较其他文件属性,请查看统计信息.

如果directory2中不存在文件但存在于directory1中,则还会输出,因为统计信息将不同.

原文链接:https://www.f2er.com/linux/400070.html

猜你在找的Linux相关文章