Is there a hard limit to the number of files a directory can have? [duplicate]

Solution 1:

It depends on your filesystem. I'm going to assume it's ext4:

The maximum number of files is global, not per directory, and it's determined by the number of inodes allocated when the filesystem was created. Try running the following command to see the number of inodes per filesystem.

    $ df -i
    Filesystem       Inodes  IUsed    IFree IUse% Mounted on
    /dev/sdb2       7864320 388119  7476201    5% /

The maximum number of subdirectories seems to be 64000 according to here (http://en.wikipedia.org/wiki/Ext4), but see also (http://kernelnewbies.org/Ext4) -- suggests that it is unlimited.

Solution 2:

It depends on the file system. ext3 suppport ~32000 subdirectories (not files!) in a given directory, with ext4 it's 64000 by default. xfs has no limit to my knowledge.

Solution 3:

I'd like add to the already correct answers that you should consider not putting too many files in a single directory. Most software doesn't handle that well (e.g. mc will be slow, many gui tools will be unusable). It's better to create a hierarchy of nested folders and distribute the files in them using some algorithm (hash of file name or content or any other method which will distribute the files equally). That's what many mature programs (e.g. squid) do.