Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, I currently have about 15 directories, each with about 33,000 files in it. I want to make a script that I can run in each directory which will sort ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    2

    Breaking up a directory with many files.

    Hello,

    I currently have about 15 directories, each with about 33,000 files in it. I want to make a script that I can run in each directory which will sort the files by ascending date (meaning starting with the oldest files and working to the newest), create a directory such as '001/' and move the oldest 3000 files there, then make another directory, and repeat until all files are moved.

    I've looked everywhere I can think of for pointers, but I haven't found anything that I can wrap my head around that works. Please help, and thank you all in advance.

    --Phlod

  2. #2
    Just Joined!
    Join Date
    Mar 2008
    Posts
    34
    making directories and moving files about isnt the hard part

    ordering by date is very dependent on which language you want to use.

    if c++ I'd probably use the STL to sort a <list> of filenames and dates

    I often have a shell of code that executes shell commands while working up a prototype.
    system - C++ Reference


    I almost sure that some bash scripting wizard could do something arcane with half a dozen lines of script and a few choice regular expression spells...

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    First stop:

    Code:
    man ls
    Outline:

    1. Sort files by time (Output could go into a *large* array, or something simple like a text file.)
    2. Create a for/while loop that moves ~3000 files, then creates a new dir, and moves 3000 more, etc.
    3. Repeat #2 until done - then (if used), delete the text file.

    Without writing the specifics:

    Code:
    ls -altr > file_list.txt
    There's the file list by modified time - oldest at the top.

    Use the last column to feed into a loop mv command. Once 3000 are moved, check that the there are still files, create a new dir, and then go back into the mv loop.

  4. #4
    Just Joined!
    Join Date
    Mar 2008
    Posts
    2
    Final code, in case anyone else ever needs. It's quick and dirty.

    #!/usr/bin/perl

    @files=`ls -tr`;
    chomp (@files);

    for($loop_count = 0; $loop_count < 11; $loop_count++) {
    system "mkdir $loop_count";
    for($count = 0; $count <= 3000; $count ++) {
    $curfile = shift (@files);
    $command = "mv " . $curfile ." " . $loop_count;
    system $command;
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...