Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a very large directory with probably millions of small files in it. It's taking forever to run ls on the directory.

Is there an easy script that I can run to split the directory into smaller ones, based on the prefixes of the filenames. My goal is to wind up with something similar to what the Debian archives' pool directory looks like.

share|improve this question
What do you know about the prefixes? It's hard to whack together a solution if you don't know any, unless you want to sort them by first letter, or brute force search through the filenames for common prefixes. – John T Mar 5 '10 at 5:00
First letter (or first two) is fine for me. I don't need special lib directories. – Ken Bloom Mar 5 '10 at 16:52
up vote 1 down vote accepted

Is this too simple? Just push every file into a single letter directory based on the file first character.


for i in `/bin/ls | xargs`
    dir=`echo $i | cut -c 1 -`
    mkdir -p $dir
    mv $i $dir

You don't get the 'lib' directories, but you could make special rules for other prefixes.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .