perl
Rev.3を表示中。最新版はこちら。
Perl Tips
スクリプトは Perl が便利だと実感、只今勉強中
ディレクトリツリーを削除
コマンドラインのrm dir_path -rfに相当するもの
use File::Path;
rmtree("$dir_path");
初めての perl プログラミング
/usr/local/hoge/hoge1/070501 のような日付付きディレクトリの古いやつを削除するスクリプト。cronで実行。
#!/usr/bin/perl
use File::Path;
$keepmonth=3;
$dir1="/usr/local/hoge/hoge1";
$dir2="/usr/local/hoge/hoge2";
$old_date=`date "-d$keepmonth month ago" +%y%m%d`;
print "clean up HogeHoge files older than $old_date";
sub clean_files {
my ($dir_path) = $_[0];
my ($DIR_HANDLE, $file, @file);
print "clean up $dir_path\n";
opendir($IN_HANDLE, $dir_path) || return;
@file = sort grep { /^[0-9]{6}$/ && -d "$dir_path/$_"} readdir($IN_HANDLE);
closedir($IN_HANDLE);
foreach $file (@file) {
if ($old_date < $file) {
last;
}
print "$dir_path/$file\n";
rmtree("$dir_path/$file");
}
}
&clean_files($dir1);
&clean_files($dir2);
