What is the maximum number of folders allowed in a folder in Linux?
It depends on what filesystem your distribution uses. If you use a newer desktop distribution (i.e. Ubuntu 9.10, a recent version of Fedora), you probably use ext4. If you use something older, it is most likely ext3.
- ext2: 31998
- ext3: 31998
- ext4: unlimited
In ext2, 32,768 according to Wikipedia - http://en.wikipedia.org/wiki/Ext2fs#File_system_limits. Depends on what file system you use.
You may want to consider alternatives to whatever it is you're trying to do. Even if you stay under the filesystem limit (e.g., 32k for ext2), storing tens of thousands of files in a single directory can be cumbersome to say the least.
One possibility is that you're trying to use the filesystem for something that would be more suited to a database. Consider storing your data in a PostgreSQL, MySQL, or even a sqlite database. There aren't too many filesystems that cope well with a huge number of files-- moving or backing up your data will be slow.
If you really need to store lots of files, consider alternative directory structures. For example, you may be able to separate your files alphabetically, e.g.:
a/aaron.dat
a/alex.dat
a/alan.dat
b/bill.dat
b/brian.dat
This approach can scale by increasing the depth of your separation, e.g.,:
a/a/aaron.dat
a/l/alex.dat
a/l/alan.dat
Or if you are entering daily log files or receiving daily deliveries, you could separate them by date, e.g.:
2009-10-04/logfile-01.txt
2009-10-05/logfile-01.txt
2009-10-05/logfile-02.txt
In this particular case, you would probably also want to cron a job to package old logs into zipped tarballs to reduce the file count.
Folder limits are why enterprise systems which have to deal with an arbitrary number of files will store in one or two subdirectories based on a hash of the filename. For example, md5() the filename, and use the first four bytes as a subfolder to store the file in; for additional levels, use the next 4 for an additional subfolder, or the next 4 after that, etc. Most systems would never exceed 2 levels deep.