把Intermediate Perl上的一个例子包装了一下。 脚本能找到一周内指定一天的文件,比如今天是周五,则可以找出指定目录下在上周六到本周五之间某天修改过的文件。
脚本命令行参数
-h 获取帮助信息
-d 找出文件后删除这些文件
-q 不要向屏幕输出内容,当你想直接删除文件而不关心哪些文件被删除的时候使用该选项
-T 指定查找哪一天被修改的文件,0-6 表示 周一 至周六 0 代表周日
-D 指定在哪个目录下查找
示例: 查找 /home/michael/tmp 目录下在周三 被修改过的文件
[60 michael@10:07:14 ~/tmp]$ easyfind -T 3 -D /home/michael/tmp/
Wed Feb 10 14:39:50 2010: /home/michael/tmp/xxx.pl
Wed Feb 10 13:47:05 2010: /home/michael/tmp/functions.pl
Wed Feb 10 14:40:22 2010: /home/michael/tmp/functions2.pl
复制代码

easyfind.tar.gz (1.12 KB) 下载次数: 1
2010-02-13 10:09
#!/usr/bin/perl
###########################################
# Author: loveky #
# E-mail:
michael.wang@rdps-china.com #
# MSN :
ylzcylx@gmail.com #
###########################################
use warnings;
use strict;
use File::Find;
use Time::Local;
use Getopt::Std;
my %options;
getopts('hdqT:D:', \%options);
if (defined $options{h}) {
&show_help();
exit;
}
die "You must specify the -T -D parameters. Type \"esayfind -h\" ",
"for more information\n"
unless (defined $options{T} and defined $options{D});
my $second_per_day = 24 * 60 * 60;
my ($sec, $min, $hour, $day, $mon, $yr, $dow) = localtime;
my $start = timelocal(0, 0, 0, $day, $mon, $yr);
while ($dow != $options{T}) {
$start -= $second_per_day;
if (--$dow < 0) {
$dow += 7;
}
}
my $stop = $start + $second_per_day;
my($gather, $show) = gather_mtime_between($start, $stop);
find($gather, $options{D});
&list_file() unless defined $options{q};
exit unless defined $options{d};
&delete_file();
sub gather_mtime_between {
my ($begin, $end) = @_;
my @files;
my $gather = sub {
my $timestamp = (stat $_)[9];
unless (defined $timestamp) {
warn "Can't stat $File::Find::name: $!, skipping\n";
return;
}
push @files, $File::Find::name if
$timestamp >= $begin and $timestamp <= $end;
};
my $fetcher = sub {@files};
($gather, $fetcher);
}
sub show_help {
print " usage: easyfind -hdq -T time -D directory\n\n";
print " -h show this help message\n";
print " -d after list the files, also delete them\n";
print " -q be quiet. do not list out the files, useful when you\n";
print " want to delete files without knowing what is to be deleted\n";
print " -T set the time the day files were modified. the available \n";
print " values are 1-6 stands for Monday, Tuesday...Saturday and\n";
print " 0 for Sunday\n";
print " -D set the directory you want to start find from\n\n";
print " Author: loveky\n";
print " E-mail: michael.wang\@rdps-china.com\n\n";
}
sub list_file {
my @files = $show->();
for my $file (@files) {
next if -d $file;
my $mtime = (stat $file)[9];
my $when = localtime $mtime;
print "$when: $file\n";
}
}
sub delete_file {
my @files = $show->();
for my $file (@files) {
next if -d $file;
unlink $file or warn "Can't delete $File::Find::name: $!\n";
}
}
复制代码

easyfind.tar.gz (1.12 KB) 下载次数:0
2010-02-13 10:09