On Fri, Feb 28, 2003 at 12:11:14AM +0000, How Can ThisBe wrote:
> I'm working on a little experimental script and I'm wondering if there
> is any kind of limit as to how many files or subdirectories a directory
> can have.

Yes, there is a limit on how many subdirectories a directory can have.
This is because there is a limit on how many hardlinks there can be to
a given file/directory and each directory has a link ("..") to its
parent.  The count for how many hardlinks there are to a given inode is
kept as a signed 16-bit integer which means that you can have a bit
over 32000 subdirectories in a given directory.
(Each of those can of course have some 32000 subdirectories and so on.)

There is no limit on how many files you can have in a directory, but if
you have too many entries in a directory it can be slow to
open/list/create files there.
This is because a directory is essentially just a file containing a
list of all files/subdirectories in it and to find a particular file a
linear search is needed.

This means that it can sometimes be faster to have a few extra levels
of subdirectories rather than just a few levels with a lot of files at
each level.

As for how many is too many, I am not sure but I think you might start
to see a slowdown after a couple of thousand entries.

(The UFS_DIRHASH kernel option reduces the impact of large number of
directory entries, at least after the first access, so then you should
be able to have several thousand entries without any noticable

> I'm in the planing stages at the moment and I'm think that I may have
> upto 4096 directories in a single directory, each of these 4096
> directories could have upto 4096 sub directories...

That should not be a problem.  Depending on how you access these
directories you *might* see some of the slowdown for large directories
that I mentioned above, so it might be a good idea to reduce these
numbers somewhat (to 2048 or 1024 maybe), but functionally it won't be
a problem. (And if you use the UFS_DIRHASH option, which I believe is
used by default in the GENERIC kernel, there should not be any
noticable slowdown either.)

(It won't be a problem for the computer anyway, but for a human trying to
look through directories it quickly becomes unwieldy once the number of
entries in a directory start exceeding a couple of hundred.) 

> The other option I have (if this is too many) is to have 256
> subdirectories (I'm guessing this on will not be a problem)

<Insert your favourite quote here.>
Erik Trulsson

To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-questions" in the body of the message

Reply via email to