How many files can I have on a single directory?

This question is related to this one.

I work with animation, which generates a LOT of files (+/- 1,000,000) typically stored on a single directory. On Mac Os X, some bugs came up with more than +/-30,000 files, so I used to break the animation into various directories.

On Ubuntu, is there a limit for the number of files a single directory can hold?


Solution 1:

Ubuntu does not limit the size of a directory, it's imposed by the file system. Each file and directory is an so-called inode. You can use df -i to check the number of inodes in use and available for all mounted filesystems.

I've just created 1 million and one files without issues because my inode limit for my ext4 home partition of 50 GB (46 GiB) is large enough.

I used shell expansion for creating the files, combined with the touch utility:

mkdir test
cd test
touch {0..300000}
touch {300000..600000}
touch {600000..900000}
touch {900000..1000000}

This creates 1000001 files which can be verified with ls | wc -l. Why 300000..600000 and not 300001..600000? Because I was too lazy to put that 1 at the end.

df -i looks like:

/dev/sda6            3055616 1133635 1921981   38% /home

Now remove the test files (cd ..&&rm -f test took much longer, so use rm with the filenames):

rm {0..300000}
rm {300000..600000}
rm {600000..900000}
rm {900000..1000000}
cd ..
rmdir f

and the number of inodes in use decreased immediately after removal of the files:

/dev/sda6            3055616  133634 2921982    5% /home

Note that even if the filesystem allows such large numbers of files, it's a horrible idea to store such large files in a single directory. At least use some subdirectories with a structure like f/i/l/e/filename.ext. Programs do often not expect such large quantities of files.