17

Wondering if there is a limit to the number of files that can be stored inside a directory, in CentOS 6. There is one particular directory which could potentially have millions of subdirectories.

Storage capacity aside, is there a limit to the number of files that can be contained in a directory? (I assume here that "file" can mean either a file or a directory).

Thanks very much!

Juan Carlos Coto
  • 677
  • 2
  • 6
  • 13
  • 1
    I believe that there is an excellent wikipedia article comparing the filesystems and items like this. – mdpc May 09 '13 at 18:45
  • 1
    @Bryan: This is not a good duplicate, because the only answer in the linked question are Wikipedia links, which is not a good kind of answer on [SF]. – Sven May 09 '13 at 21:11
  • @Svw, maybe not a good duplicate, but it's still a duplicate. You could always improve the answer to the other question by adding information from the linked wiki page if you think that it will improve it. – Bryan May 10 '13 at 20:04

3 Answers3

25

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.

wangj
  • 366
  • 2
  • 3
  • Ext4 has no limit if the `dir_index` and `dir_nlink` features are set. (They are the default for new file systems.) Old directories need to be upgraded to `dir_nlink` by running `e2fsck -f -D`. – Georg Schölly Dec 04 '17 at 08:34
  • Query `dir_index` and `dir_nlink` by running `tune2fs -l /dev/sdb2` and check the field `Filesystem features:` – Basil A Jun 16 '20 at 09:30
12

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.

Sven
  • 97,248
  • 13
  • 177
  • 225
  • 4
    Ext4 has no limit if the `dir_index` and `dir_nlink` features are set. (They are the default for new file systems.) Old directories need to be upgraded to `dir_nlink` by running `e2fsck -f -D`. – Georg Schölly Dec 04 '17 at 08:34
6

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.

skarap
  • 733
  • 5
  • 7