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?

Aaron Esau
  • 365
  • 1
  • 4
  • 13

1 Answers1

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.

Julie Pelletier
  • 7,562
  • 1
  • 20
  • 43
  • 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 04:46
  • Awesome answer. Any external references? – Aaron Esau Dec 30 '16 at 05:36
  • 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 08: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 08: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 08:50
  • How do I find the number of `inode`s I have? Or, how can I find the number of possible nested directories? – Aaron Esau Dec 30 '16 at 09:02
  • @JohanMyréen What about built-ins? Those don't require `execve`. – phk Dec 30 '16 at 11:42
  • 2
    @Arin Try `df -i`. – Johan Myréen Dec 30 '16 at 12:05
  • 1
    @phk There is no restriction on command line length for built-ins. Other limits, like PATH_MAX may apply, though. – Johan Myréen Dec 30 '16 at 12:15
  • @JohanMyréen Thanks. I changed the command to `df -ih .` so it shows only current partition. – Aaron Esau Dec 30 '16 at 22:37
  • In the second `folder` example I posted, is that also limited by the number of `inode`s? And also, what happens when it runs out of `inode`s? – Aaron Esau Jan 04 '17 at 05:04
  • No, that's a different question. The size of directory (file) has limitations based on the filesystem used. http://stackoverflow.com/a/466596/5207302 explains that pretty well. Do note that having big directories can yield to serious performance issues for operations as simple as opening a file from that directory. – Julie Pelletier Jan 04 '17 at 05:10
  • @Arin When you run out of inodes you get a "File system full" error, just like when you run out of disk space. – Barmar Jan 04 '17 at 18:32
  • @Arin The inode limit applies to the total number of files and directories in a filesystem. How they're organized is irrelevant. – Barmar Jan 04 '17 at 18:33