Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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.

#!/bin/sh


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

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.