Moves all log files in a directory into new directories based on the name(s) of the original files.
Example: (output from Unix 'script command')
5:49:24 hancock6@server4 /home/hancock6# cd logs
5:49:28 hancock6@server4 /home/hancock6/logs# ls
20020305.tar.gz 20020324.tar.gz 20020412.tar.gz 20020501.tar.gz 200203
+06.tar.gz 20020325.tar.gz 20020413.tar.gz 20020502.tar.gz 20020307.ta
+r.gz 20020326.tar.gz 20020414.tar.gz 20020503.tar.gz 20020308.tar.gz
+20020327.tar.gz 20020415.tar.gz 20020504.tar.gz 20020309.tar.gz 20020
+328.tar.gz 20020416.tar.gz 20020505.tar.gz 20020310.tar.gz 20020329.t
+ar.gz 20020417.tar.gz 20020506.tar.gz 20020311.tar.gz 20020330.tar.gz
+ 20020418.tar.gz 20020507.tar.gz 20020312.tar.gz 20020331.tar.gz 2002
+0419.tar.gz 20020508.tar.gz 20020313.tar.gz 20020401.tar.gz 20020420.
+tar.gz 20020509.tar.gz 20020314.tar.gz 20020402.tar.gz 20020421.tar.g
+z 20020510.tar.gz 20020315.tar.gz 20020403.tar.gz 20020422.tar.gz 200
+20511.tar.gz 20020316.tar.gz 20020404.tar.gz 20020423.tar.gz 20020512
+.tar.gz 20020317.tar.gz 20020405.tar.gz 20020424.tar.gz 20020513.tar.
+gz 20020318.tar.gz ... etc
5:49:29 hancock6@server4 /home/hancock6/logs# mvlog.pl
5:49:40 hancock6@server4 /home/hancock6/logs# ls -R
.: 200203/ 200204/ 200205/ access_log error_log mvlog.pl*
200203: 20020305.tar.gz 20020312.tar.gz 20020319.tar.gz 20020326.tar.g
+z 20020306.tar.gz 20020313.tar.gz 20020320.tar.gz 20020327.tar.gz 200
+20307.tar.gz 20020314.tar.gz 20020321.tar.gz 20020328.tar.gz 20020308
+.tar.gz 20020315.tar.gz 20020322.tar.gz 20020329.tar.gz 20020309.tar.
+gz 20020316.tar.gz 20020323.tar.gz 20020330.tar.gz 20020310.tar.gz 20
+020317.tar.gz 20020324.tar.gz 20020331.tar.gz 20020311.tar.gz 2002031
+8.tar.gz 20020325.tar.gz
200204: 20020401.tar.gz 20020409.tar.gz 20020417.tar.gz 20020425.tar.g
+z 20020402.tar.gz 20020410.tar.gz 20020418.tar.gz 20020426.tar.gz 200
+20403.tar.gz 20020411.tar.gz 20020419.tar.gz 20020427.tar.gz 20020404
+.tar.gz 20020412.tar.gz 20020420.tar.gz 20020428.tar.gz 20020405.tar.
+gz 20020413.tar.gz 20020421.tar.gz 20020429.tar.gz ... etc
Hope this gives an idea of what it does! It's pretty much hardwired to what I needed it to do as well (as in name formats for the files/dirs to create), but maybe someone might find it of use :)
#!/usr/bin/perl
#
# move files from a log dir
# into a newly created directory
# based on the name of the logfiles
# 20020305.tar.gz
use strict;
my (@logfiles);
# file cntaining all the logfiles:
my $logdir="/home/hancock6/logs";
#open the dir ready for reading:
opendir(LOGDIR, $logdir);
#read in files from dir:
my @logdir=readdir(LOGDIR);
for my $file (@logdir){
# foreach file in the dir, if it's a tarball,
# copy it into @logfiles:
if($file=~/(.*)\.tar\.gz$/){
push(@logfiles, $file);
print $file,"\n";
}
}
for my $logfile (@logfiles){
# foreach logfile in @logfiles,
# build the dirname to copy it to:
$logfile=~/(.*)\.tar\.gz$/;
my $dirname=substr($1,0,-2);
$dirname=$logdir."/".$dirname;
print $dirname,"\n";
# check if the dir exists, if not, create it:
if(!-e $dirname){
mkdir($dirname, 0755);
}
# copy the file into the dir:
link ($logdir."/".$logfile, $dirname."/".$logfile);
# remover the old file:
unlink($logdir."/".$logfile);
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.