#!/usr/bin/perl -w
use strict;
use Time::localtime;
use File::Find;
use File::Copy;
my $vmlogpath = $ARGV[0] or die "y'all didn't enter no arguments";
find(\&sortFile, $ARGV[0]);
sub sortFile {
if ( -f ) {
my $filename=$_;
$filename =~ /([0-9]{5,})/;
my $unixtime = $1 or print "Not a log file: $filename\n";
my $dirname=makeDirName($unixtime);
unless(-e "${ARGV[0]}/${dirname}/${filename}") {
my $path=makeDir($ARGV[0], $dirname);
moveFile($filename, $path);
print "$filename is the file name.\n";
print "$unixtime is the extracted unixtime.\n";
print "$dirname is the directory name.\n";
print "$path is the path.\n";
}
}
else { print "Not a plain file: $_\n"; }
}
sub moveFile {
my $tehfile=$_[0];
my $tehdir=$_[1];
move($tehfile, $tehdir) or die "Could not move files!\n$!";
return 1;
}
sub makeDir {
my $currentdir=$_[0];
my $newdir=$_[1];
my $newpath="${currentdir}/${newdir}";
unless (-d $newpath) {
mkdir($newpath,755) or die "mkdir failed for $newpath: $!\n"
}
return $newpath;
}
sub makeDirName {
my $tehunix=$_[0];
my $day_of_year=localtime(int($tehunix))->yday;
my $week_of_year=int($day_of_year / 7) + 1;
my $year=1900+localtime(int($tehunix))->year;
return $year . $week_of_year;
}
Refactorings
No refactoring yet !
hourback
November 21, 2008, November 21, 2008 15:33, permalink
Modified the capitalization of subroutines.
#!/usr/bin/perl -w
use strict;
use Moose;
use Time::localtime;
use File::Find;
use File::Copy;
my $vmlogpath = $ARGV[0] or die "y'all didn't enter no arguments";
find(\&sort_file, $ARGV[0]);
sub sort_file {
if ( -f ) {
my $filename=$_;
$filename =~ /([0-9]{5,})/;
my $unixtime = $1 or print "Not a log file: $filename\n";
my $dirname=make_dir_name($unixtime);
unless(-e "${ARGV[0]}/${dirname}/${filename}") {
my $path=make_dir($ARGV[0], $dirname);
move_file($filename, $path);
print "$filename is the file name.\n";
print "$unixtime is the extracted unixtime.\n";
print "$dirname is the directory name.\n";
print "$path is the path.\n";
}
}
else { print "Not a plain file: $_\n"; }
}
sub move_file {
my $tehfile=$_[0];
my $tehdir=$_[1];
move($tehfile, $tehdir) or die "Could not move files!\n$!";
return 1;
}
sub make_dir {
my $currentdir=$_[0];
my $newdir=$_[1];
my $newpath="${currentdir}/${newdir}";
unless (-d $newpath) {
mkdir($newpath,755) or die "mkdir failed for $newpath: $!\n"
}
return $newpath;
}
sub make_dir_name {
my $tehunix=$_[0];
my $day_of_year=localtime(int($tehunix))->yday;
my $week_of_year=int($day_of_year / 7) + 1;
my $year=1900+localtime(int($tehunix))->year;
return $year . $week_of_year;
}
I have a huge amount of log files on my hard drive and regularly download more. All of the files end up in a single directory. The log file names contain UNIX timestamps. I wrote this script to execute in a given directory and then move the found files to a new path based on the UNIX timestamp. Each new directory is named after a year and the week of the year, e.g. 200801, the first week of 2008. (Actually, the month doesn't have a leading zero yet. I just realized.)