Making 99 folders 99 folders deep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Making 99 folders 99 folders deep
# 8  
Old 04-26-2011
Perhaps something like the following:
Code:
#!/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

Last edited by alister; 04-26-2011 at 04:44 AM..
# 9  
Old 04-27-2011
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
# 10  
Old 04-27-2011
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
# 11  
Old 04-27-2011
When i run the code i get
Code:
 line 5: [: 1: unary operator expected
usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]
           [reps [begin [end [s]]]]

# 12  
Old 04-27-2011
Also works fine with ksh 1993-12-28 p.

Perhaps you invoked it incorrectly or made a faulty modification.
# 13  
Old 04-27-2011
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.
# 14  
Old 04-27-2011
Quote:
Originally Posted by YukonAppleGeek
When i run the code i get
Code:
 line 5: [: 1: unary operator expected
usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]
           [reps [begin [end [s]]]]

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

Quote:
Originally Posted by YukonAppleGeek
EDIT: Also you forgot = after ndirs
No, I did not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy files/folders and show the files/folders?

Hi, So i know we use cp -r as a basic to copy folders/files. I would like this BUT i would like to show the output of the files being copied. With the amazing knowledge i have i have gone as far as this: 1) find source/* -exec cp -r {} target/ \; 2) for ObjectToBeCopied in `find... (6 Replies)
Discussion started by: Imre
6 Replies

2. Shell Programming and Scripting

Exclude certain folders

Hi Experts, Below is my shell script and it will move the files older than 90 days to archive mount. Now my new requirement is , I need to move some of the directory files older than 365 days. How can I achieve this. Simply I have DIR1 DIR2 DIR3 DIR4 I need to exclude DIR 2 and DIR 2... (5 Replies)
Discussion started by: arumugavelvelu
5 Replies

3. UNIX for Dummies Questions & Answers

Archive folders and sub folders

Hi Can i archive folder and folders in with the tar command My files are located in subfolders Eg: Folder1/Folder1_1/*.pdf Folder1/Folder1_2/*.pdf Folder1/Folder1_3/*.pdf so i would like to tar all the files in Folder1_1 and Folder1_2 only not Folder1_3 that should be done next... (2 Replies)
Discussion started by: cnrj
2 Replies

4. Shell Programming and Scripting

Copy between two different folders containing same sub-folders

I have a folder like this ls input1 dir1 dir2 dir3 file1 file2 file3 dir1, dir2 and dir3 are sub-folders inside the folder input1 ls input2 dir1 dir2 dir3 file1 file2 file3 My dir1 in input1 folder has files f1, f2, f3 and f4. My dir1 in input2 folder has file f4 and f5. ... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Solaris

Folders with more than one 'owner'

I have a peculiar problem. I have a particular directory with the following characteristics: -bash-3.00# ls -lah total 18 drwxr-x--- 7 gandalf shire 512 jul 3 07:20 . drwxr-x--- 11 gandalf shire 512 mai 10 2010 .. drwxr-xr-x 6 gandalf shire 3,0K jul 24 19:25 brasdeff drwxr-xr-x... (9 Replies)
Discussion started by: poyato
9 Replies

6. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

7. Shell Programming and Scripting

How to hide folders

hello everybody, i would like to hide visibility of the folders , i.e. not to giving any physically visibility to any users . Is there any way to do it other than changing the permission and adding "." post folder name . by changing the permission , we cann't do any activity , but have... (1 Reply)
Discussion started by: manas_ranjan
1 Replies

8. Shell Programming and Scripting

Renaming folders

Hello, I'm new to unix and I have to rename all folder fron the current folder from a name like "xx - Name of the Folder" to "Name of the Folder - xx". xx is a number ... Can somebody, please, help? Thank you! (6 Replies)
Discussion started by: mirciulicai
6 Replies

9. UNIX for Dummies Questions & Answers

Copying Folders without some folders... ;-)

I am in a fix....... I have to write a backup script to backup say Folder A. Folder A contains n folders 1,2 ,3 .....n. my script should copy A without folder 2 & 3. Is there anyway I can do it without writing individual copy commands???? Please help.... (5 Replies)
Discussion started by: chimpu
5 Replies

10. Shell Programming and Scripting

Backing up Folders without some folders...;)

I am in a fix....... I have to write a backup script to backup say Folder A. Folder A contains n folders 1,2 ,3 .....n. my script should copy A without folder 2 & 3. Is there anyway I can do it without writing individual copy commands???? Please help.... (1 Reply)
Discussion started by: chimpu
1 Replies
Login or Register to Ask a Question