2011-03-07 108 views
0

如何查找tree拥有的用户和tree拥有的组?我怎样才能找到一个完整的目录,其中文件归tree目录中的文件权限

+2

看看'File :: Find' – 2011-03-07 15:55:23

+0

我编辑了你的问题来澄清你的意思(我相信)。 (“树”作为用户/组名的选择引起了我的警惕,因为它也可以表示目录结构......) – Cascabel 2011-03-07 15:57:20

回答

3

File::Find模块是一个标准的Perl模块(即它可以在Perl的所有安装中使用)。您可以使用File :: Find来浏览目录树并搜索您想要的文件。

要使用,您可以创建一个wanted子例程来分析这些文件,然后让子程序find在其调用中包含该例程。 File::Find模块有点奇怪,因为它最初只是用于find2perl命令。

这是一些完全未经测试的代码。注意你喜欢使用全局变量和包变量。这是我不喜欢File::Find的原因之一。

use File::Find; 
our $myUid = getpwnam('tree'); 
our $muGid = getgrnam('tree'); 
find (\&wanted, @dirList); 

sub wanted { 
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($File::Find::name); 
    next if (not -f $File::Find::name); 
    next if ($uid != $myUid); 
    next if ($gid != $myGid); 
    print qq(File "$File::Find::name" is owned by group 'tree' and user 'tree'\n); 
} 

我写我自己File::Find称为File::OFind,因为它更多地是面向对象的。你可以从here得到。这比较容易理解。 (同样,经过充分测试):

use File::OFind; 
# Really should test if these return something 
my $myUid = getpwnam('tree'); 
my $muGid = getgrnam('tree'); 

# Create your directory search object 
my $find = File::OFind->new(STAT => 1, $directory); 

# Now keep looping and examining each file 
while($find->Next) { 
    next if ($find->Uid != $myUid); 
    next if ($find->Gid != $myGid); 
    next if ($find->Type ne "f"); #Is this a file? 
    print $find->Name . " is owned by group and user tree\n"; 
} 
0

您将需要完成此任务的内置Perl函数包括​​,getgrnamstat

($name,$passwd,$uid,$gid, 
    $quota,$comment,$gcos,$dir,$shell,$expire) = getpwnam 'tree'; 

将返回大量关于用户tree的有用信息。对于此任务,您将对$uid字段特别感兴趣。同样,

($name,$passwd,$gid,$members) = getgrnam 'tree'; 

检索有关组01​​的数据。您将对$gid字段最感兴趣。最后,stat功能

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, 
    $atime,$mtime,$ctime,$blksize,$blocks) 
     = stat($filename); 

返回与关于文件(或目录)的系统信息的13个元素的阵列。对于您的任务,您正在查找文件,以便从stat($filename)返回的用户和​​组ID与从​​和getgrnam返回的用户和​​组ID匹配。

0

文件::查找::规则使得这个干净而简单:

use File::Find::Rule; 

my $uid_tree = getpwnam('tree'); 
my $gid_tree = getgrnam('tree'); 

my @files = 
    File::Find::Rule 
    ->file() 
    ->uid($uid_tree) 
    ->gid($gid_tree) 
    ->in('.'); 

编号: