Visit Our UNIX and Linux User Community


creating a directory tree


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting creating a directory tree
# 1  
Old 10-07-2009
"interactively" create a directory tree of n dir in m levels

Hi all,

I'd like to create a directory tree, and define from stdin how many levels deep and how many directories in each level should be created.
What I wrote does not work properly:
Code:
#!/bin/bash
#set -x

read -p " What root directory? " rootDir
[ -d $rootDir ] && { /bin/rm -R $rootDir; mkdir $rootDir; } || mkdir $rootDir
read -p " How many directories? " dirnb
read -p " How many levels? " levnb

l=0
generate() {
   ((l++))
   for (( n=0; n < ${dirnb:-1}; n++ )); do
      mkdir $1/directory$n
   done
   while [ $l -lt ${levnb:-1} ]; do
      for directory in $1/*; do
         generate $directory
      done
   done
}
generate $rootDir
find $rootDir

the result is:
Code:
 What root directory? testwx
 How many directories? 2
 How many levels? 3
testwx
testwx/directory1
testwx/directory1/directory1
testwx/directory1/directory0
testwx/directory0
testwx/directory0/directory1
testwx/directory0/directory1/directory1
testwx/directory0/directory1/directory0
testwx/directory0/directory0
testwx/directory0/directory0/directory1
testwx/directory0/directory0/directory0

I should get
Code:
testwx
testwx/directory1
testwx/directory1/directory1
testwx/directory1/directory1/directory1
testwx/directory0/directory1/directory0
testwx/directory1/directory0
testwx/directory1/directory0/directory1
 testwx/directory0/directory1/directory0
testwx/directory0
testwx/directory0/directory1
testwx/directory0/directory1/directory1
testwx/directory0/directory1/directory0
testwx/directory0/directory0
testwx/directory0/directory0/directory1
testwx/directory0/directory0/directory0

What am I missing?

Thanks in advance

PS: it's not homework!

Last edited by NBaH; 10-07-2009 at 11:26 PM..
# 2  
Old 10-08-2009
Hi NBaH,

When cooking something recursive I think there are two essential ingredients:
  • local variables ( "typeset var" )
  • variable passing
There are multiple functions active at the same time and their variables should not interfere with parent's variables. To make a distinction between local and global variables I used lower case and upper case. Also there was a while loop in the generate function that should be an if statement.

Code:
#!/bin/bash
#set -x

read -p " What root directory? " ROOTDIR
[ -d $ROOTDIR ] && { /bin/rm -R $ROOTDIR; mkdir $ROOTDIR; } || mkdir $ROOTDIR
read -p " How many directories? " DIRNB
read -p " How many levels? " LEVNB

generate() {
   typeset path=$1
   typeset level=$2
   typeset directory
   typeset n
   ((level++))
   for (( n=0; n < ${DIRNB:-1}; n++ )); do
      mkdir $path/directory$n
   done
   if [ $level -lt ${LEVNB:-1} ]; then
      for directory in $path/*; do
         generate $directory $level
      done
   fi
}
LEVEL=0
generate $ROOTDIR $LEVEL
find $ROOTDIR



---------- Post updated at 11:10 PM ---------- Previous update was at 10:33 PM ----------

Also, if you move the creation of the topdir at each level to the function, you can make it a bit shorter:

Code:
#!/bin/bash

read -p " What root directory?     " ROOTDIR
read -p " How many subdirectories? " SUBDIRS
read -p " How many levels?         " LEVELS
if [ -d $ROOTDIR ]; then
  echo $ROOTDIR exist
  exit 1
fi

generate() {
   typeset path=$1
   typeset level=$2
   typeset n
   mkdir $path
   if (( level > 0 )); then
     for (( n=0; n < SUBDIRS; n++ )); do
        generate $path/directory$n $((level-1))
     done
   fi
}

generate $ROOTDIR $LEVELS
find $ROOTDIR

# 3  
Old 10-08-2009
MySQL [solved] "interactively" create a directory tree of n dir in m levels

Hi Scrutinizer,

help typeset says it's obsolete, and to use declare instead.
As you pointed to the differences between global and local variables, I finally used local builtin, which seems to be more appropriate in this case, isn't it.

I thank you very much.

Previous Thread | Next Thread
Test Your Knowledge in Computers #396
Difficulty: Medium
HP-UX was the first Unix to offer access control lists for file access permissions as an alternative to the standard Unix permissions system.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To do directory tree search

Hello Everyone, I need to find the file / directory with the maximum timestamp in a directory tree having many files / directories. Could you please help. Thanks, H squared (3 Replies)
Discussion started by: H squared
3 Replies

2. Shell Programming and Scripting

Specific directory parsing in a directory tree

Hi friends, Hello again :) i got stuck in problem. Is there any way to get a special directory from directory tree? Here is my problm.." Suppose i have one fix directory structure "/abc/xyz/pqr/"(this will be fix).Under this directory structure i have some other directory and... (6 Replies)
Discussion started by: harpal singh
6 Replies

3. Shell Programming and Scripting

help need while creating tree structure

Hi Experts, I have table in mysql like below: 'user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `parent_id` varchar(100) NOT NULL, `member_name` varchar(100) NOT NULL, `city` varchar(100) NOT NULL, `member_id` varchar(100) NOT NULL, `password` varchar(100) NOT... (1 Reply)
Discussion started by: naw_deepak
1 Replies

4. UNIX for Dummies Questions & Answers

How to copy a tree of directory

Mi question is how can you copy only de three of directory and not the files in it. Only a need the three of directorys not the files (6 Replies)
Discussion started by: enkei17
6 Replies

5. UNIX for Dummies Questions & Answers

directory tree with directory size

find . -type d -print 2>/dev/null|awk '!/\.$/ {for (i=1;i<NF;i++){d=length($i);if ( d < 5 && i != 1 )d=5;printf("%"d"s","|")}print "---"$NF}' FS='/' Can someone explain how this works..?? How can i add directory size to be listed in the above command's output..?? (1 Reply)
Discussion started by: vikram3.r
1 Replies

6. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management. Thanks in advance- Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies

7. Shell Programming and Scripting

directory tree

Hi all, The following is a script for displaying directory tree. D=${1:-`pwd`} (cd $D; pwd) find $D -type d -print | sort | sed -e "s,^$D,,"\ -e "/^$/d"\ -e "s,*/\(*\)$,\:-----\1,"\ -e "s,*/,: ,g" | more exit 0 I am trying to understand the above script.But... (3 Replies)
Discussion started by: ravi raj kumar
3 Replies

8. Shell Programming and Scripting

Searching directory tree

I'm currently trying to write a script that will do the following: search a given directory tree for a file with MMDDYYYY in the name. delete those files only. I can't figure out how to make the script delete the files with the MMDDYYYY in the filename after finding them. Should I export... (7 Replies)
Discussion started by: blane
7 Replies

9. Programming

directory as tree

hi i have modified a program to display directory entries recursively in a tree like form i need an output with the following guidelines: the prog displays the contents of the directory the directory contents are sorted before printing so that directories come before regular files if an entry... (2 Replies)
Discussion started by: anything2
2 Replies

10. Shell Programming and Scripting

Creating breadth traversal binary tree

I almost have the entire script written. however the problem is how would i assign the global variable to terminate the process from the bottom up to ensure the child terminates so the parent can. ex. I am proccess 1 I am proccess 2 etc Here is the code $ cat tree.c ... (3 Replies)
Discussion started by: slurpeyatari
3 Replies

Featured Tech Videos