Server Fault is a question and answer site for system and network administrators. 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

looking for a way to pre-create directories on linux to be used to store a large number of files.

We will be generating file ids using a GUID - I need to keep a copy of these files on a linux web server. I plan on using subdirectories to split up the files (it's ext3)... so for example, the filename 055c102b-62fb-4671-a3c7-68b9515ec53e.swf would live in /data/files/0/5/5/055c102b-62fb-4671-a3c7-68b9515ec53e.swf (taking the first 3 characters as directory names)

My question is - how to create the /data/files/?/?/?/ directories ahead of time? Where ? could be a-z or 0-9

share|improve this question
up vote 13 down vote accepted

Using Bash I think it is:

mkdir -p {{a..f},{0..9}}/{{a..f},{0..9}}/{{a..f},{0..9}}

Might make more sense to just to create them as needed though.

I think since it is hex, this gives 16^x directories for x = tree depth, but someone from stackoverflow can probably explain that better and/or confirm if that is accurate.

share|improve this answer
That's exactly it. – Dennis Williamson May 7 '10 at 14:35
Very cool - thank you so much! Works perfectly. The reason I (think I) need to create them ahead of time is because the external process that will be uploading these files via scp/sftp assumes the directories exist. Do you see any downsides to doing this? – Erik Sorensen May 7 '10 at 14:54
I guess the only real downside that comes to mind is that maybe in theory access will be a tad bit slower as each level of the tree will be a little bit wider until it is full. That sounds like BS to me though as a I type it (especially with dir_index enabled on ext3/4), so no, no real downside that I can think of. I would recommend that maybe you go over to stackoverflow and ask if 3 is the right tree depth if you haven't already given that a lot of thought (Can't really hurt, at least not too much). – Kyle Brandt May 7 '10 at 15:04

Your Answer


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.