Command to segregate files by first letter

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Basically I have a folder with thousands of files in it and I want to split those files into folders. I could go through and do this ~26 times,

BASH Code: [ Select ]
mkdir _a
mv a* _a/
mv _a/ a/
  1. mkdir _a
  2. mv a* _a/
  3. mv _a/ a/


Even if I recall the last commands I used for each letter it's still a hassle though.

It would be nice to have a BASH script I could call something like this instead,

BASH Code: [ Select ]
segregate /path/to/directory
  • kc0tma
  • o|||||||o
  • Web Master
  • User avatar
  • Posts: 3318
  • Loc: Trout Creek, MT

Post 3+ Months Ago

You could do a perl script to make it happen. Just a rough quickie guess:

PERL Code: [ Select ]
my @alphabet = "a".."z";
 
foreach (@alphabet) {
  my $tmpdir = "_" . $_;
  my $folder = $_;
  my $md = "mkdir " . $_;
  my $copyfile = "mv " . $_ . "*";
  my $copyfolder = "mv " . $tmpdir . " " . $folder;
 
  system($md);
  system($copyfile);
  system($copyfolder);
}
 
  1. my @alphabet = "a".."z";
  2.  
  3. foreach (@alphabet) {
  4.   my $tmpdir = "_" . $_;
  5.   my $folder = $_;
  6.   my $md = "mkdir " . $_;
  7.   my $copyfile = "mv " . $_ . "*";
  8.   my $copyfolder = "mv " . $tmpdir . " " . $folder;
  9.  
  10.   system($md);
  11.   system($copyfile);
  12.   system($copyfolder);
  13. }
  14.  


Them just make that script executable and stick in your /usr/local/bin and run it. That code looks like a dog chewed on it and I didn't test it or anything, I pretty much just farted it out. But you get the basic idea I hope.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Yeah I've just about got a quickie written in PHP, but if there's already a command something like this it would be awesome. :D

//edit -- Now that I'm done, here's what I used.

PHP Code: [ Select ]
if($argc < 2)
{
   echo "USAGE: ./segregate directory1[, directory2, directoryN...]\n";
}
 
$switches      = array();
$directories   = array();
$subs       = 'abcdefghijklmnopqrstuvwxyz';
$subs_mask     = strtoupper($subs) . "{$subs}1234567890";
 
for($i = 1; $i < $argc; $i++)
{
   if($argv[$i][0] == '-')
   {
      $switches[] =& $argv[$i];
   }
   else
   {
      $directories[] =& $argv[$i];
   }
}
 
// TODO switches/options
 
foreach($directories as $dir)
{
   if(is_dir($dir))
   {
      for($i = 0; $i < 26; $i++)
      {
         mkdir("$dir/{$subs[$i]}");
      }
      mkdir("$dir/0-9");
      mkdir("$dir/misc");
     
      $dh  = opendir($dir);
      while (false !== ($f = readdir($dh)))
      {
         if(strspn($f, $subs_mask))
         {
            $d = strspn($f[0], '1234567890') ? '0-9' : strtolower($f[0]);
            if(is_file("$dir/$f") || (is_dir("$dir/$f") && strlen($f) > 1 && $f != '0-9' && $f != 'misc'))
            {
               rename("$dir/$f", "$dir/$d/$f");
            }
         }
         else if($f != '.' && $f != '..')
         {
            rename("$dir/$f", "$dir/misc/$f");
         }
      }
      closedir($dh);
   }
   unset($files);
}
  1. if($argc < 2)
  2. {
  3.    echo "USAGE: ./segregate directory1[, directory2, directoryN...]\n";
  4. }
  5.  
  6. $switches      = array();
  7. $directories   = array();
  8. $subs       = 'abcdefghijklmnopqrstuvwxyz';
  9. $subs_mask     = strtoupper($subs) . "{$subs}1234567890";
  10.  
  11. for($i = 1; $i < $argc; $i++)
  12. {
  13.    if($argv[$i][0] == '-')
  14.    {
  15.       $switches[] =& $argv[$i];
  16.    }
  17.    else
  18.    {
  19.       $directories[] =& $argv[$i];
  20.    }
  21. }
  22.  
  23. // TODO switches/options
  24.  
  25. foreach($directories as $dir)
  26. {
  27.    if(is_dir($dir))
  28.    {
  29.       for($i = 0; $i < 26; $i++)
  30.       {
  31.          mkdir("$dir/{$subs[$i]}");
  32.       }
  33.       mkdir("$dir/0-9");
  34.       mkdir("$dir/misc");
  35.      
  36.       $dh  = opendir($dir);
  37.       while (false !== ($f = readdir($dh)))
  38.       {
  39.          if(strspn($f, $subs_mask))
  40.          {
  41.             $d = strspn($f[0], '1234567890') ? '0-9' : strtolower($f[0]);
  42.             if(is_file("$dir/$f") || (is_dir("$dir/$f") && strlen($f) > 1 && $f != '0-9' && $f != 'misc'))
  43.             {
  44.                rename("$dir/$f", "$dir/$d/$f");
  45.             }
  46.          }
  47.          else if($f != '.' && $f != '..')
  48.          {
  49.             rename("$dir/$f", "$dir/misc/$f");
  50.          }
  51.       }
  52.       closedir($dh);
  53.    }
  54.    unset($files);
  55. }

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 6 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.