Sponsored Content
Full Discussion: creating a directory tree
Top Forums Shell Programming and Scripting creating a directory tree Post 302360015 by Scrutinizer on Thursday 8th of October 2009 03:10:25 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
MKZFTREE(1)							  H. Peter Anvin						       MKZFTREE(1)

NAME
mkzftree - Create a zisofs/RockRidge compressed file tree SYNOPSIS
mkzftree [OPTIONS]... INPUT OUTPUT DESCRIPTION
Takes an input file tree (INPUT) and create a corresponding compressed file tree (OUTPUT) that can be used with an appropriately patched mkisofs(8) to create a transparent-compression ISO 9660/Rock Ridge filesystem using the "ZF" compression records. OPTIONS
-f, --force Always compress all files, even if they get larger when compressed. -z level, --level level Select compression level (1-9, default is 9). Lower compression levels are faster, but typically result in larger output. -u, --uncompress Uncompress an already compressed tree. This can be used to read a compressed filesystem on a system which cannot read them natively. -p parallelism, --parallelism parallelism Compress in parallel. The parallelism value indicates how many compression threads are allowed to run. -x, --one-filesystem Do not cross filesystem boundaries, but create directory stubs at mount points. -X, --strict-one-filesystem Do not cross filesystem boundaries, and do not create directory stubs at mount points. -C path, --crib-path path Steal ("crib") files from another directory if it looks (based on name, size, type and modification time) like they match entries in the new filesystem. The "crib tree" is usually the compressed version of an older version of the same workload; this thus allows for "incremental rebuilds" of a compressed filesystem tree. The files are hardlinked from the crib tree to the output tree, so if it is desirable to keep the link count correct the crib path should be deleted before running mkisofs. The crib tree must be on the same filesystem as the output tree. -l, --local Do not recurse into subdirectories, but create the directories themselves. -L, --strict-local Do not recurse into subdirectories, and do not create directories. -F, --file Indicates that INPUT may not necessarily be a directory; this allows operation on a single file. Note especially that if -F is specified, and INPUT is a symlink, the symlink itself will be copied rather than whatever it happens to point to. -s, --sloppy Treat file modes, times and ownership data as less than precious information and don't abort if they cannot be set. This may be useful if running mkisofs on an input tree you do not own. -v, --verbose Increase the program verbosity. -V value, --verbosity value Set the program verbosity to value. -q, --quiet Issue no messages whatsoever, including error messages. This is the same as specifying -V 0. -h, --help Display a brief help message. -w, --version Display the release version. BUGS
Long options (beginning with --) may not work on all systems. See the message printed out by mkzftree -h to see if this applies to your system. Inode change times (ctimes) are not copied. This is a system limitation and applies to all file copy programs. If using the parallel option (-z) the access times (atimes) on directories may or may not be copied. If it is important that the atimes on directories are copied exactly, avoid using -z. AUTHOR
Written by H. Peter Anvin <hpa@zytor.com>. COPYRIGHT
Copyright (C) 2001-2002 H. Peter Anvin. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICU- LAR PURPOSE. SEE ALSO
mkisofs(8) zisofs-tools 30 July 2001 MKZFTREE(1)
All times are GMT -4. The time now is 10:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy