13

I'm curious, how many folders can be nested, and why? Is there a limit?

What I mean by nested is when folders are in this structure:

folder
 |_ folder
     |_ folder
         |_ folder
             |_ ...

Not like this:

folder
 |_ folder
 |_ folder
 |_ folder
 |_ ...

If there is a limit, is it set by the operating system, or by the file system?

20

The limit will be the number of inodes on your partition since directories, like regular files, take an inode each.

Nothing would stop you from creating a directory inside a directory inside another directory and so on until you run out of inodes.

Note that the shell's command line does have a maximum length which can cause issues with really long paths, but it would still be possible to cd progressively towards the target file.

  • 2
    Indeed, by experiment this is what mkdir -p does and it allows to exceed MAX_PATH. Such files can't be opened by their canonical path, but one can create them without problems. – DepressedDaniel Dec 30 '16 at 4:46
  • 1
    It's pretty logical, actually. A relative path starts at the current working directory, an inode. Inodes are not arranged hierarchically: there is no difference in this respect between a directory that is linked deep in the name hierachy and the file system's root directory. – Johan Myréen Dec 30 '16 at 8:04
  • 3
    @JuliePelletier The POSIX (and Linux) API defines a maximum PATH_MAX (4096 on Linux), that is much shorter than the maximum length of the command line. And while we are nitpicking: the maximum command line is not really a shell restriction. The limit is imposed by the argument vector length to the execve system call, and so applies to any program. – Johan Myréen Dec 30 '16 at 8:11
  • 1
    @JohanMyréen: Those are good observations but my point was that it would still be accessible relatively, no matter how deep it goes. – Julie Pelletier Dec 30 '16 at 8:50
  • 2
    @Arin Try df -i. – Johan Myréen Dec 30 '16 at 12:05

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.