如何比较两个文件夹内容的所有者和权限? 有没有像diff命令recursion地比较两个文件夹和显示所有者和权限的差异?
与所有东西一样,解决scheme是一个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 ); }
如果目录2中不存在文件,但目录1中存在文件,则会有输出,因为该stat会有所不同。
ls -al将显示权限,如果两者都在相同的文件夹中,您将得到如下所示的内容:
drwxr-xr-x 4 root root 4096 nov 28 20:48 temp drwxr-xr-x 2 lucas 1002 4096 mrt 24 22:33 temp2
第三列是所有者,第四列是组。
你确定2个文件夹在某种程度上应该是相同的recursion吗? 我认为rsync命令是非常强大的。 在你的情况下,你可以运行:
rync -n -rpgov src_dir dst_dir (-n is a must in case dst_dir will be modified )
不同的文件或文件夹将被列为命令输出
你可以看到man rsync的这些选项的更完整的解释)