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 PATH_MAX bytes.

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.

  • 1
    PATH_MAX isn't really the maximum overall path length, but (IIRC) the maximum path length accepted by a system call. I don't know how that works with the hashing functions, but e.g. something like this works on my Debian (if slowly): d=abcdefghijklmnopqrstu; for i in {1..999}; do echo $i; mkdir $d; cd $d; done. The resulting path is 21983 bytes long, as shown by Bash's pwd | wc. (Maybe I should have tried to see if /bin/pwd croaks on it)
    – ilkkachu
    Jul 4 '20 at 8:59
  • @ilkkachu But you'll get File name too long if you for example ls one of the directories with the length above 4095.So yes looks like that's the limit of what a syscall can handle not what the filesystem can handle.You just have to select one directory at a time like what rm -rf would do.It doesn't cause any error and can remove all the subdirs successfully.Thanks for pointing that out. Jul 4 '20 at 10:18
  • @ParsaMousavi, yes. Keeping track of the length of the name of a whole path, or just the nesting depth itself would seem to be rather difficult, since we can rename directories at any time (making the name of that subtree longer), move directories inside others (changing the nesting depth of everything in that subtree), and probably most problematically also mount filesystems at an arbitrary depth. Related post I stumbled upon: eklitzke.org/path-max-is-tricky
    – ilkkachu
    Jul 4 '20 at 16:04
  • @ilkkachu Also feel free to post your thoughts as an answer. Jul 4 '20 at 17:22

According to ext(4) man page, section about dir_nlink

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 tune2fs.

Other Sources:

  • 3
    Although your answer is totally correct , but my question was about nested directory depth not the max number of subdirs possible.However thanks for attention. Jul 4 '20 at 9:45
  • Very good, but did you read the question. Jul 4 '20 at 21:58
  • @ctrl-alt-delor Yep, question read I did. Admittedly there are moments where I do not, but for this particular one I did. And over the course of last 6 or so years on SE network I think I've learned that a lot of it is guesswork to find out what OP really wants; nested directories and subdirectories are essentially the same term, yet somehow even here OP believes it to be different. Jul 4 '20 at 23:11
  • 1
    They asked about nesting depth, you answered about nesting breadth. I think it is better to ask for clarity than to guess what is wanted. That way we help people to ask better questions, and we can give better answers. Jul 5 '20 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.