2011-03-07 64 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('.'); 

編號: