This is my rather convoluted way of ensuring that the following is true: 1. No more than x number of files in my 'to' directory 2. Newest files will be given the highest precedence in the move 3. Zero length files will be killed 4. Only move .log files via glob. If anyone wants to use or improve, feel free, since I think a hash would have been a lot less complex than an array with a sort and regex substring match....ick.
#!/usr/bin/perl -w #This file sees how many files are currently in a target directory use File::Copy; chdir "/tmp/to/"; @filesto = glob("*.log"); $numFiles = scalar @filesto; #maxfiles needs to be 1 larger than your maximum value due to perl usi +ng a 0 based index for arrays. $maxfiles = 5 - $numFiles; if ($maxfiles <=0) { exit; } chdir "/tmp/from"; @filesfrom = glob("*.log"); $counter=0; foreach (@filesfrom) { ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filesfrom[$counter]); #Unlink files that are zero bytes prior to copy. if ($size eq 0) { unlink $filesfrom[$counter]; $counter--; next; } $filestoCreated[$counter] = $ctime . " " .$filesfrom[$counter]; $counter++; } #@sortedlist = reverse sort @filestoCreated; #This is a particularly nasty sort which essentially uses a regex as s +ubstring match to only sort the numeric part of my hacked up array. E +nsures that we get #a numeric sort only. @sortedlist = sort { ($b =~ /(^\d+)/)[0] <=> ($a =~ /(^\d+)/)[0] || uc($a) cmp uc($b) } @filestoCreated; #This section ensures that the system will never copy more than the ma +x number of files defined in maxfiles. $loopcounter=scalar (@sortedlist); if ($loopcounter > $maxfiles) { $loopcounter = $maxfiles-1; print "Loopcounter is greater than than maxfiles: $maxfiles Loopc +ounter: $loopcounter\n"; } $countfinals=0; while ($countfinals<$loopcounter) { my @execute= split / /, $sortedlist[$countfinals]; system "/bin/mv /tmp/from/$execute[1] /tmp/to/$execute[1]"; $countfinals++; }
|
|---|