我正在尝试删除服务器上的日志文件。但是在脚本实际删除文件之前,我看不到要删除哪些文件。我的问题是,有没有办法显示哪些文件会被删除而不实际删除它们?最好是通过参数。
像这样:
perl -- DeleteLogs.pl C:\Logs 14 -safeRun
C:\ Logs 是保存日志的路径。
14 是天数。早于此参数的每个文件都应删除。
-safeRun 这应该是告诉脚本仅打印出将删除哪些文件但尚未删除的文件的参数。
脚本
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Scalar::Util qw(looks_like_number);
use File::Basename;
use local::lib;
use Data::Dumper qw(Dumper);
use File::Basename;
use DateTime;
use DateTime::Format::Strptime;
use DateTime::Duration;
use POSIX qw(strftime);
use Time::Local;
use Time::Piece;
use Time::Seconds 'ONE_WEEK';
my $logPath = $ARGV[0];
my $leaveLog = $ARGV[1];
my $file;
my $filename;
if (not defined $logPath) {
die "Need a correct path\n";
}
if (-d $logPath) {
print "Directory exist\n";
if (looks_like_number($leaveLog)) {
print "Everything correct";
DoDir($logPath);
}
else {
die "$leaveLog isn't a correct Integer"
}
}
else {
die "Directory don't exist\n"
}
sub DoDir {
my $dir = shift;
opendir(DIR, $dir) || die "Unable to open $dir: $!";
my(@files) = grep {!/^\.\.?$/} readdir(DIR);
closedir(DIR);
foreach (@files) {
if (-d ($file = "$dir\\$_")) {
print "Found a directory: $file\n";
DoDir($file);
}
else {
checkDate($file);
}
}
}
sub checkDate {
$filename = basename($file);
#Regex for pattern "Normalfall"
#Bsp: 2019_07_02_TEST_SERVER_ALL.log
if ($filename =~ m/^\D?20\d{2}_\d{2}_\d{2}_[\w_]+.log$/) {
#Regex: "\D?" match any char thats not a digit.
# "\d{2}\_" expecting 2 random digits and underscore
# "[\w_]+." expecting 1 to unlimited word char. matches with underscore char
# "log" matches with the characters log (case sensitive)
NormalFile();
}
#wl-deploy-181205082159_TEST-server_server-2019.log
elsif($filename =~ m/^wl-deploy-\d+_[\w\-_.]+\d+\.log$/) {
#Regex: "^" asserts position at start of a line
# "wl-deploy-" matches the character (case sensitive)
# "\d+" matches with a digit [0-9]
# "[\w\-_.]+"
# "\w" matches any word char [a-zA-Z0-9]
# "\-" matches the char -
# "_." matches a single char in the list _.
DeployFile();
}
#2019_06_30_TestAdapter_BackendLog.log.gz_2019-07-03
elsif ($filename =~ m/^\D?20(\d{2}_){3}[\w_]+\.log\.gz_\D?20(\d{2}-){2}\d{2}$/) {
#print "$filename: File match .log.gz\n";
}
#stdout.log.190529100348-190605092829.gz_2019-06-07
elsif ($filename =~ m/^stdout\.log(\.\w+?-\w+?(\.gz_\d{4}-\d{2}-\d{2})?)?$/) {
#Regex: "stdout" matches the chars
# "\." matches the char .
# "log" macthes the char log (case sensitive)
# "(\.\w+?-\w+?(\.gz_\d{4}-\d{2}-\d{2})?)?"
# "?" matches between zero and one times, as many times as possible
# "\w+?" matches aby word chars
# "+?" matches between one and unlimited times, as few times as possible
# "(\.gz_\d{4}-\d{2}-\d{2})?" 2nd Capturing Group
# "\d{4}" matches a digit (equal to [0-9])
# "\d{2}" matches a digit (equal to [0-9])
# "\d{2}" matches a digit (equal to [0-9])
StdoutFile();
#print "$filename: File match stdout\n";
}
#2019-01-13-18-45.wl-test.error
elsif ($filename =~ m/^\D?20\d{2}\-\d{2}\-\d{2}\-\d{2}\-\d{2}\.[\w\-]+\.error$/) {
#Regex: "[\w\-]"
# "\w" matches any word character
# "\-" matches the character - literally
ErrorFile();
}
#TT_TestAccessImpl_Statistic0.log.lck
elsif ($filename =~ m/^[\w]{2}\_[\w]+\.log\.lck$/) {
#Regex:
LckFile();
}
#server.log00335
elsif ($filename =~ m/^server\.log(\d{5})?$/) {
#print "$filename: File Match server\n";
ServerFile();
}
#access.log00021
elsif ($filename =~ m/^access\.log(\d{5})?$/) {
#print "$filename: File match access\n";
}
else{
print "$filename: don't match\n";
}
}
sub NormalFile() {
my $chars = substr($filename, 0 , 10);
my $date_parser = DateTime::Format::Strptime->new(
pattern => '%Y_%m_%d',
on_error => 'croak',
);
my $dt = $date_parser->parse_datetime($chars);
my $day14 = DateTime->now->subtract (days => $leaveLog);
my $cmp = DateTime->compare($day14, $dt);
if ($cmp == -1 || $cmp == 0) {
print "don't delete\n";
}
else {
print "delete File: $file\n";
unlink $file;
}
}
sub DeployFile() {
$filename =~ s/[^\d]//g;
my $dateFromString = substr($filename, 0 , 6);
my ($year, $month, $day) = unpack("(a2)*", $dateFromString);
$year = "20".$year;
my $dateFromFile = $year."_".$month."_".$day;
my $date_parser = DateTime::Format::Strptime->new(
pattern => '%Y_%m_%d',
on_error => 'croak',
);
my $dt = $date_parser->parse_datetime($dateFromFile);
my $day14 = DateTime->now->subtract (days => $leaveLog);
#print "$day14\n";
my $cmp = DateTime->compare($day14, $dt);
#print "$cmp\n";
if ($cmp == -1 || $cmp == 0) {
print "don't delete\n";
}
else {
print "delete File: $file\n";
unlink $file;
}
}
sub LckFile {
print "$filename delete\n";
}
sub ErrorFile {
$filename = substr($filename, 0 , 10);
$filename =~ s/-/_/g;
my $date_parser = DateTime::Format::Strptime->new(
pattern => '%Y_%m_%d',
on_error => 'croak',
);
my $dt = $date_parser->parse_datetime($filename);
my $day14 = DateTime->now->subtract (days => $leaveLog);
#print "$day14\n";
my $cmp = DateTime->compare($day14, $dt);
#print "$cmp\n";
if ($cmp == -1 || $cmp == 0) {
print "don't delete\n";
}
else {
print "delete File: $file\n";
unlink $file;
}
}
sub StdoutFile {
print "$filename\n";
$filename =~ /^stdout\.log\.((?:\d+?)|unknown)-(\d+?)(?:\.gz_\d{4}-\d{1,2}-\d{1,2})?$/g;
unless (defined $1 and defined $2) {
warn "Log file '$filename' does not match regex\n";
return;
}
if ($1 ne "unknown") {
my $firstDate = substr($1, 0, 6);
my ($year, $month, $day) = unpack("(a2)*", $firstDate);
$year = "20" . $year;
$firstDate = $year . "_" . $month . "_" . $day;
}
my $secondDate = substr($2, 0, 6);
my ($year, $month, $day) = unpack("(a2)*", $secondDate);
$year = "20" . $year;
$secondDate = $year . "_" . $month . "_" . $day;
my $date_parser = DateTime::Format::Strptime->new(
pattern => '%Y_%m_%d',
on_error => 'croak',
);
my $dt = $date_parser->parse_datetime($secondDate);
my $day14 = DateTime->now->subtract ( days => $leaveLog);
#print "$day14\n";
my $cmp = DateTime->compare($day14, $dt);
#print "$cmp\n";
if ($cmp == -1 || $cmp == 0) {
print "don't delete\n";
}
else {
print "delete File $file\n";
unlink $file;
}
}
sub ServerFile {
#print "$filename delete\n";
}
答案 0 :(得分:2)
我会
1)添加新的全局数组my @dfiles;
2)将所有unlink $file;
语句更改为push @dfiles, $file;
3)在显示文件的main末尾添加代码:
print "These are the files that would be deleted:\n";
for (@dfiles) {print; print "\n"}
我将由您决定如何处理文件是否真正被删除(通过切换还是通过y / n提示符)。但是,如果要删除文件,则只需:
for (@dfiles) {unlink $_}