Is there any limit for the maximum nested directories in the ext4 filesystem? For example ISO-9660 filesystem AFAIK cannot have more than 7 level of sub-directories.
There isn’t any limit inherent in the file system design itself, and experimentation (thanks ilkkachu) shows that directories can be nested to a depth exceeding limits one might naïvely expect (
PATH_MAX, 4096 on Linux, although that limits the length of paths passed to system calls and can be worked around with relative paths).
Part of the implementation apparently assumes that the overall path length, inside a given file system, never goes above
PATH_MAX; see the directory hashing functions which allocate
The only directory-related limit which seems to be checked in the file system implementation is the length of an individual path component, which is limited to 255 bytes; but that doesn’t have any bearing on the nested depth.
According to ext(4) man page, section about
Normally, ext4 allows an inode to have no more than 65,000 hard links. This applies to regular files as well as directories, which means there can be no more than 64,998 subdirectories in a directory (because each of the '.' and '..' entries, as well as the directory entry for the directory in its parent directory counts as a hard link). This feature lifts this limit by causing ext4 to use a link count of 1 to indicate that the number of hard links to a directory is not known when the link count might exceed the maximum count limit.
In other words, if
dir_nlink feature is enabled - there is no limit, otherwise -- 64k subdirs. You can check if this feature is enabled with
sudo dumpe2fs /dev/sda1 | grep --color=always dir_nlink. It can be disabled, possibly for compatibility with legacy systems via