답변:
@oHessling 은 거의 그것을 가지고 있다고 생각 합니다 : ls를 구문 분석하지 마십시오 .bash 에서 더 많은 것을 할 수 있습니다 :
four_days=$(date -d "4 days ago" +%Y%m%d)
for f in ABC_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].log; do
date=${f#ABC_}
date=${date%.log}
(( $date < $four_days )) && rm "$f"
done
four_days=$(echo "puts [clock format [clock scan {4 days ago}] -format %Y%m%d]" | tclsh)
한 가지 방법으로 perl
:
내용 script.pl
:
use warnings;
use strict;
use Time::Local qw/timelocal/;
use File::Spec;
## Process all input files.
while ( my $file = shift @ARGV ) {
## Remove last '\n'.
chomp $file;
## Extract date from file name.
my ($date) = $file =~ m/.*_([^.]+)/ or next;
## Extract year, month and day from date.
my ($y,$m,$d) = $date =~ m/(\d{4})(\d{2})(\d{2})/ or next;
## Get date in seconds.
my $time = timelocal 0, 0, 0, $d, $m - 1, $y - 1900 or next;
## Get date in seconds five days ago.
my $time_5_days_ago = time - 5 * 24 * 3600;
## Substract them, and if it is older delete it and print the
## event.
if ( $time - $time_5_days_ago < 0 ) {
unlink File::Spec->rel2abs( $file ) and printf qq[%s\n], qq[File $file deleted];
}
}
그것을 테스트하기 위해 몇 가지 파일을 만듭니다.
touch ABC_20120430.log ABC_20120502.log ABC_20120320.log ABC_20120508.log ABC_20120509.log
로 확인하십시오 ls -1
:
ABC_20120320.log
ABC_20120430.log
ABC_20120502.log
ABC_20120508.log
ABC_20120509.log
script.pl
다음과 같이 스크립트를 실행하십시오.
perl script.pl *.log
다음과 같은 출력으로 :
File ABC_20120320.log deleted
File ABC_20120430.log deleted
File ABC_20120502.log deleted