清单 5. 用于删除旧文件的脚本
#!/usr/local/bin/perlmy $choice = shift;my @files = @ARGV;my @selection;if ($choice =~ /thismonth/){ my ($day,$mon,$year) = dateaslist(); my $match = sprintf('%04d%02d',$year,$mon); foreach my $file (@files) { if ($file =~ m/$match/ && $choice eq 'thismonth') { push @selection,$file; } elsif ($file !~ m/$match/ && $choice eq 'notthismonth') { push @selection,$file; } }}elsif ($choice =~ /today/){ my ($day,$mon,$year) = dateaslist(); my $match = sprintf('%04d%02d%02d',$year,$mon,$day); foreach my $file (@files) { if ($file =~ m/$match/ && $choice eq 'today') { push @selection,$file; } elsif ($file !~ m/$match/ && $choice eq 'nottoday') { push @selection,$file; } }}elsif ($choice =~ /last(/d+)days/){ my $days = $1; my ($day,$mon,$year) = dateaslist(time()-($1*24*3600)); my $match = sprintf('%04d%02d%02d',$year,$mon,$day); my $spec = sprintf('last%ddays',$days); my $notspec = sprintf('notlast%ddays',$days); foreach my $file (@files) { my ($date) = ($file =~ m/(/d{8})/); push @selection,$file if ($date >= $match && $choice eq $spec); push @selection,$file if ($date < $match && $choice eq $notspec); }}print join ' ',@selection;sub dateaslist{ my ($time) = @_; $time = time() unless defined($time); my ($day,$mon,$year) = (localtime($time))[3..5]; $mon++; $year+= 1900; return($day,$mon,$year);} |
使用这个脚本,您可以通过各种方法挑选出备份文件(请参见清单 6)。
清单 6. 挑选备份文件
$ filesbydate.pl last5days # Files created in the last 5 days$ filesbydate.pl notlast14days # Files 15 days or older$ filesbydate.pl nothismonth # Files not created this month |
;请记住,比较操作针对于文件名,而不是文件系统的创建或修改日期,所以该脚本可以对头天晚上创建的文件进行操作。
标签: