Is there a hard limit to the number of files a directory can have?
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.
Related videos on Youtube
Juan Carlos Coto
Updated on September 18, 2022Comments
-
Juan Carlos Coto over 1 year
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!
-
mdpc almost 11 yearsI believe that there is an excellent wikipedia article comparing the filesystems and items like this.
-
FooBee almost 11 years@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 Server Fault.
-
Kcmamu almost 11 years@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.
-
-
David Stocking over 6 yearsExt4 has no limit if the
dir_index
anddir_nlink
features are set. (They are the default for new file systems.) Old directories need to be upgraded todir_nlink
by runninge2fsck -f -D
. -
David Stocking over 6 yearsExt4 has no limit if the
dir_index
anddir_nlink
features are set. (They are the default for new file systems.) Old directories need to be upgraded todir_nlink
by runninge2fsck -f -D
. -
Basil A almost 4 yearsQuery
dir_index
anddir_nlink
by runningtune2fs -l /dev/sdb2
and check the fieldFilesystem features: