Making 99 folders 99 folders deep

I am trying to make a unix shell script that will make 99 folders 99 deep (counting the first level folders). So far i have made it make the first 99 folders and 99 more in all of the folders. The only problem is the only way i have found is copying and pasting part of the script over and over and over.

Here is code any suggestions:

#!/bin/bash
for num in {1..99}; do
    mkdir $num
    cd $num
    for num2 in {1..99}; do
	mkdir $num2
    done
    cd ..
done

Code for 3 folder levels

#!/bin/bash
mdir=$(pwd)
for num in {1..99}; do
    mkdir $num
    cd $num
    for num2 in {1..99}; do
	mkdir $num2
        cd $num2
        for num3 in {1..99}; do
	    mkdir $num3
        done
        cd ..
    done
    cd $mdir
done

Make 9 folders 4 deep (found at: *new so cant post url but google "10000 folders applescript" and it is the first link )

#!/bin/bash
mkdir 0
mkdir 0/0
mkdir 0/0/0
mkdir 0/0/0/0

for a in 1 2 3 4 5 6 7 8 9
do
for b in 1 2 3 4 5 6 7 8 9
do
for c in 1 2 3 4 5 6 7 8 9
do
for d in 1 2 3 4 5 6 7 8 9
do
cp -R 0/0/0/0/ 0/0/0/$d
done
cp -R 0/0/0/ 0/0/$c
done
cp -R 0/0/ 0/$b
done
cp -R 0/ $a
done
exit 0

EDIT: After making all folders have 3 levels it took 23 min so maybe not 99 deep configurable deepness if posable?

That's a very good call. I'm not sure the universe is up to creating that many directories. 99 levels of 99 directories in 99 directories is a LOT of directories: 99^99 = 3.69729638 � 10^197

Allowing for the creation of one directory per nanosecond (unrealistically swift, 99^3 in 23 minutes is nearly 1.5 milliseconds) and suspending all other physical limitations, it would take:
99^99 * 10^-9 / 60 / 60 / 24 / 365 = 1.17240499 � 10^181 years

Many, many, many orders of magnitude beyond current estimates of the universe's age.

As to your original problem, a recursive shell function would probably be the simplest approach.

Regards and welcome to the forum,
Alister

I tried doing a loop but when ever i do it does not work, it never changes the directory and then errors up.

To be very frank, I didnt get the issue here. You created 99 folders with each folder having 99 subfolders, got that part. Now whats troubling you?
I also didn't understand the term "99 folders 99 deep" - please explain.

regards,
Ahamed

I am trying to make 5 sub folders with out having to copy/paste/reformat the code. I know it is posable but i am wondering if there is a easer way of do that, i am also some what new to unix shell scripting. (I also will probably not do 99 folders more like 20-50)

If your intention is to implement the algorithm in your original post, you really need to come to terms with the numbers you'd be dealing with. Based on how far you got in 23 minutes, and generously slashing the time per directory from 1.5 milliseconds to 1 millisecond flat, the following is approximately how long it would take to create n levels of n directories in each directory:

20:  3 trillion millenia
15:  14,000 millenia
10:  4 months
 9:  4.5 days
 8:  4.5 hours
 7:  14 minutes
 6:  47 seconds
 5:  3 seconds

It blows up quickly, so you'll probably want to keep it small. If you don't mind saying, why are you doing this? I'm curious.

Regards,
Alister

The arithmetic:
((((((20^20) * .001) / 60) / 60) / 24) / 365) / 1 000 = 3.32501268 � 10^12
((((((15^15) * .001) / 60) / 60) / 24) / 365) / 1 000 = 13,885.5242
(((((10^10) * .001) / 60) / 60) / 24) / 30 = 3.85802469
((((9^9) * .001) / 60) / 60) / 24 = 4.48403344
(((8^8) * .001) / 60) / 60 = 4.66033778
((7^7) * .001) / 60 = 13.7257167
(6^6) * .001 = 46.65600
(5^5) * .001 = 3.12500

I am just wondering if there is any way of loop the mkdir code instead of typing it over and over and over again. I am trying to learn a little about how to write unix shell scripts and how it works. *It is a lot more powerful then thought.:slight_smile:

Perhaps something like the following:

#!/bin/sh

ndirs() (
    l=$l.
    if [ ${#l} -gt $n ]; then
        return 0
    fi
    cd "$1"
    for i in $(jot $n); do
         mkdir $i
         ndirs $i
    done
)

l=               # Current level
n=$1             # How many levels / How many dirs per level
d=${2:-$PWD}     # Starting directory

ndirs "$d"

This approach is not at all efficient. It constantly spawns subshells. But it is succinct since the changes to the current working directory are not seen by parent shells.

The script takes two arguments. The first is mandatory, the number of levels (which is also the number of directories per level). The second is optional, the starting point for directory creation (defaults to the current directory).

The jot can be replaced with seq or with some other facility that your shell may provide for creating a list from a range for the for-loop.

Also, in my earlier calculations, I underestimated the number of directories created. Yes. Those estimates were actually low. The number of directories is not n^n, but n + n^2 + n^3 + ... + n^n.

Regards,
Alister

I cant get it to work there is no errors but i does nothing. Also variables have to go before they are called or there will be errors.
EDIT: Also you forgot = after ndirs

I tested it before I posted it and it worked just fine. I used bash 2.05b on OSX 10.4.11.

Variables do not need to be declared before they are used in sh. If an undeclared variable is referenced, it expands to an empty string. However, even if you were correct, that script does not use any undeclared variables. The function is not invoked until the last line of the script.

Alister

When i run the code i get

 line 5: [: 1: unary operator expected
usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]
           [reps [begin [end ]]]

Also works fine with ksh 1993-12-28 p.

Perhaps you invoked it incorrectly or made a faulty modification.

I ran the raw code do i need to add any thing. Give example please, i am on Mac OS X Snow Leopard if that helps.

You need to at least supply it with a number on the command line. If the script is saved as ndirs.sh, call it with ndirs.sh 3 . It should only take a couple of seconds and should generate a tree of directories under the current directory.

---------- Post updated at 01:46 AM ---------- Previous update was at 01:43 AM ----------

No, I did not.

Ok! It works but i cant change the # of directory in each folder.

That's just a very minor tweak. Add another argument to the script (so that it doesn't use the same one for both levels and # of dirs per level) and then modify the jot command to use the new argument.

I tryed changeing n in

    for i in $(jot $n); do
         mkdir $i
         ndirs $i
    done
)

to

    for i in $(jot $dnum); do
         mkdir $i
         ndirs $i
    done
)

and dnum is

dnun='3'

But all i got was the jot usage

dnum and dnun are two different variables.

I fell stupid, it works now. :S