<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mariano Peterson &#187; perl code</title>
	<atom:link href="http://petersonpages.com/mariano/blog/tag/perl-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://petersonpages.com/mariano/blog</link>
	<description>Software engineer. Cycling fanatic. Snowboarder. Surfer. And very proud father of two boys.</description>
	<lastBuildDate>Thu, 08 Oct 2009 05:42:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Organizing log files into date based directories</title>
		<link>http://petersonpages.com/mariano/blog/2008/04/10/rename-files-in-bulk/</link>
		<comments>http://petersonpages.com/mariano/blog/2008/04/10/rename-files-in-bulk/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 16:01:26 +0000</pubDate>
		<dc:creator>Mariano</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[perl code]]></category>

		<guid isPermaLink="false">http://petersonpages.com/mariano/blog/?p=6</guid>
		<description><![CDATA[I recently had to grep through a few hundred thousand log files in a single directory. The logs dated back several years and had never been organized into directories. In fact, there were so many log files that grep&#8217;ing the directory resulted in an &#8220;argument list too long&#8221; error. This error is described clearly here, [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to grep through a few hundred thousand log files in a single directory.  The logs dated back several years and had never been organized into directories.  In fact, there were so many log files that grep&#8217;ing the directory resulted in an &#8220;argument list too long&#8221; error.  This error is described clearly <a href="http://www.moundalexis.com/archives/000035.php">here</a>, and the quickest solution is to use find to pipe the filenames into grep using xargs:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*.log&quot;</span> <span style="color: #660033;">-print0</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;xyz&quot;</span></pre></div></div>

</p>
<p><span id="more-6"></span></p>
<p>I still wanted to clean things up so I decided to organize the logs into date based directories.  I wrote a Perl script that scans files in a directory, parses a date from each filename (YYYY_MM_DD_&#8230;), and moves the file to the appropriate directory.  For example, the script moves 2008_03_01_foo.txt to 2008/03/01/2008_03_01_foo.txt and creates the directories if necessary.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> File<span style="color: #339933;">::</span><span style="color: #006600;">Path</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$year</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$month</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$day</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dir</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;*&gt;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$year</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$month</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$day</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(\d+)_(\d+)_(\d+)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%04d/%02d/%02d&quot;</span> <span style="color: #339933;">,</span><span style="color: #0000ff;">$year</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$month</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$day</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mkpath<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dir</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!-</span>d <span style="color: #0000ff;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">rename</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;$dir/$_&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!-</span>d <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Unable to move $_:  $!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
</p>
]]></content:encoded>
			<wfw:commentRss>http://petersonpages.com/mariano/blog/2008/04/10/rename-files-in-bulk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

