mkdir while true loop stops at 406 folders

Solution 1:

In your original question, you try to create a file hierarchy like this:

.
├── infinite2
│   ├── infinite1
...
│ (endless level deep)  ├── infinite2

and your script starts getting errors when your "depth" reaches the 406th "level". There is no known file system that can hold so many levels of sub-directories as the ones you want.

However, in your comments, it seems that you want a directory hierarchy like this:

.
├── dir000000001
├── dir000000002
...
├── dir124999999
└── dir125000000

Although it may be possible to create such a huge number of single level sub-directories under a directory using a script like this:

#!/bin/bash
let i=0
while (( i < 125000 )) ; do
  mkdir $(printf "dir%06d" $i){000..999}
  let i++
done

it would be very very very (did I say enough times very?) inefficient to create and use them. I was able to test this script by creating more than two million directories; but 125 million directories are way too many.

One better alternative would be to create a three-level structure, each level holding a thousand sub-directories in a hierarchy like this:

.
├── dir000
│   ├── 000
│   │   ├── 000
│   │   ├── 001
│   │   ├── 002
...
│   │   └── 999
│   ├── 001
│   │   ├── 000
│   │   ├── 001
...
│   │   └── 999
...
│   ├── 999
│   │   ├── 000
...
│   │   └── 999
...
├── dir124
│   ├── 000
...
│   ├── 999
│   │   ├── 000
...
        └── 999

The following script can be used to create these:

#!/bin/bash
let i=0
while (( i < 125000 )) ; do
  let a=i/1000
  let b=i%1000
  mkdir -p $(printf "dir%03d/%03d/\n" $a $b){000..999}
  let i++
done

Even in this case, it would be very very very difficult to use so many directories.

Another problem is that you may easily run out of inodes in your file system: You can get the infamous No space left on device error, while your current file system does have storage space, but no inode space is left to create a new file or directory. Please, check the IFree column in the df -i . command output, before running the above script.

So, again, I think this is a typical XY problem that would require a completely different approach.