Want to improve this question? Update the question so it s on-topic for Stack Overflow.
Closed 11 years ago.
根据我在另一个职位上的调查结果和建议:。 如何排除索里斯·指挥中一条全局航道清单,我已决定撰写该书的每字版,并看到我如何能够优化其操作,使之比本土发现的指挥更快。 迄今为止,结果令人印象深刻!
该书的目的是报告所有未拥有的档案和目录,说明遵守审计准则的情况。 文字必须接受一份目录和档案清单,以排除(无论是通过完整途径还是用野心名称),而且必须尽可能少地处理。 其用意是,我们(我所工作的公司)支持数百个“不星”系统,并且能够运行所有这些“不星”系统(多功能职业顾问、多个平台:AIX、HP-UX、Solari和Ltlin),而没有我们首先安装或升级任何东西。 换言之,它必须拥有标准图书馆和我们期望所有系统的双筒。
我尚未对书面论点作出说明,因此,所有论点都难以在文字中加以表述。 我计划最终提出以下论点,并可能利用机会这样做:
-d = comma delimited list of directories to exclude by path name
-w = comma delimited list of directories to exclude by basename or wildcard
-f = comma delimited list of files to exclude by path name
-i = comma delimited list of files to exclude by basename or wildcard
-t:list|count = Defines the type of output I want to see (list of all findinds, or summary with count per directory)
这里是我迄今这样做的来源:
#! /usr/bin/perl
use strict;
use File::Find;
# Full paths of directories to prune
my @exclude_dirs = ( /dev , /proc , /home );
# Basenames or wildcard names of directories I want to prune
my $exclude_dirs_wildcard = .svn ;
# Full paths of files I want to ignore
my @exclude_files = ( /tmp/test/dir3/.svn/svn_file1.txt , /tmp/test/dir3/.svn/svn_file2.txt );
# Basenames of wildcard names of files I want to ignore
my $exclude_files_wildcard = *.tmp ;
my %dir_globs = ();
my %file_globs = ();
# Results will be sroted in this hash
my %found = ();
# Used for storing uid s and gid s present on system
my %uids = ();
my %gids = ();
# Callback function for find
sub wanted {
my $dir = $File::Find::dir;
my $name = $File::Find::name;
my $basename = $_;
# Ignore symbolic links
return if -l $name;
# Search for wildcards if dir was never searched before
if (!exists($dir_globs{$dir})) {
@{$dir_globs{$dir}} = glob($exclude_dirs_wildcard);
}
if (!exists($file_globs{$dir})) {
@{$file_globs{$dir}} = glob($exclude_files_wildcard);
}
# Prune directory if present in exclude list
if (-d $name && in_array(@exclude_dirs, $name)) {
$File::Find::prune = 1;
return;
}
# Prune directory if present in dir_globs
if (-d $name && in_array(@{$dir_globs{$dir}},$basename)) {
$File::Find::prune = 1;
return;
}
# Ignore excluded files
return if (-f $name && in_array(@exclude_files, $name));
return if (-f $name && in_array(@{$file_globs{$dir}},$basename));
# Check ownership and add to the hash if unowned (uid or gid does not exist on system)
my ($dev,$ino,$mode,$nlink,$uid,$gid) = stat($name);
if (!exists $uids{$uid} || !exists($gids{$gid})) {
push(@{$found{$dir}}, $basename);
} else {
return
}
}
# Standard in_array perl implementation
sub in_array {
my ($arr, $search_for) = @_;
my %items = map {$_ => 1} @$arr;
return (exists($items{$search_for}))?1:0;
}
# Get all uid s that exists on system and store in %uids
sub get_uids {
while (my ($name, $pw, $uid) = getpwent) {
$uids{$uid} = 1;
}
}
# Get all gid s that exists on system and store in %gids
sub get_gids {
while (my ($name, $pw, $gid) = getgrent) {
$gids{$gid} = 1;
}
}
# Print a list of unowned files in the format PARENT_DIR,BASENAME
sub print_list {
foreach my $dir (sort keys %found) {
foreach my $child (sort @{$found{$dir}}) {
print "$dir,$child
";
}
}
}
# Prints a list of directories with the count of unowned childs in the format DIR,COUNT
sub print_count {
foreach my $dir (sort keys %found) {
print "$dir,".scalar(@{$found{$dir}})."
";
}
}
# Call it all
&get_uids();
&get_gids();
find(&wanted, / );
print "List:
";
&print_list();
print "
Count:
";
&print_count();
exit(0);
如果你想在你的系统上测试它,就简单地建立一个带有通用文档的测试目录结构,把整个树木与测试用户相混,然后删除用户。
I ll take any hints, tips or recommendations you could give me.
快乐!