<?xml version="1.0" encoding="windows-1252"?>
<node id="1014326" title="Re: Finding and sorting files in massive file directory" created="2013-01-20 15:38:23" updated="2013-01-20 15:38:23">
<type id="11">
note</type>
<author id="341121">
dave_the_m</author>
<data>
<field name="doctext">
You don't make it clear whether all these files are in a single directory, or in a directory heirarchy. I'm going to assume the former. Most shell commands and much perl code will appear to hang forever, since it will attempt to read the entire directory listing into memory, then sort it, before doing anything else. What you need (in general terms)
is the following perl code, which must be run from the directory in question:
&lt;code&gt;
#!/usr/bin/perl

use warnings;
use strict;

opendir my $dir, '.' or die "opendir .: $!\n";
my $file;
my $count = 0;
while (defined($file = readdir($dir))) {
    # give yourself some progression feedback
    $count++;
    print "file $count ...\n" unless $count % 1000;

    # skip all files not begining with b
    next unless $file =~ /^b/;

    # if you've created directories, may need to skip them;
    # this will slow things down, so don't do so unless necessary
    next unless -f $file;

    # do something with the file

    rename $file, "b/$file" or die "rename $file b/$file: $!\n";
}
&lt;/code&gt;
This example deals with directory entries at the most efficient and lowest level. In this case, it just moves all files starting with "b" into the subdirectory b/.
&lt;p&gt;
Obviously it needs adapting to your particular needs. For example, the rename could become&lt;p&gt;
&lt;c&gt;system "gzip", $file;&lt;/c&gt;
&lt;p&gt;
Dave.</field>
<field name="root_node">
1014320</field>
<field name="parent_node">
1014320</field>
</data>
</node>
