LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-09-2016, 04:58 AM   #1
genezing
LQ Newbie
 
Registered: Jun 2016
Posts: 4

Rep: Reputation: Disabled
Bash script to make directories recursive with depth and size


Hello,

I was wondering how one makes a bash script given a depth and a size/layer to make directories in directories and so on. For example I pass depth = 3 and size = 2 than the output needs to look like:

Code:
├── dir1
│** ├── dir3
│** │** └── dir7
│   │   └── dir8
│   ├── dir4
│   │   └── dir9
│   │   └── dir10
├── dir2
│   ├── dir5
│   │   └── dir11
│   │   └── dir12
│   ├── dir6
│   │   └── dir13
│   │   └── dir14
But I don't kow how I could program this recursive, my code that I have now is with static depth:

Code:
for (( g=1; g<=$size; g++ ))
do
for (( f=1; f<=$size; f++ ))
do
for (( e=1; e<=$size; e++ ))
do
for (( d=1; d<=$size; d++ ))
do
for (( c=1; c<=$size; c++ ))
do

mkdir -p foo/$g/$f/$e/$d
done
done
done
done
done
greetings
 
Old 06-09-2016, 06:07 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,372
Blog Entries: 3

Rep: Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184
If you're using bash you could use sequence expressions:

Code:
mkdir -p dir{1..2}/dir{3..6}/dir{7..14}/
That will give you 74 directories. They won't quite get the numbering you describe above, but is it close enough?
 
Old 06-09-2016, 06:15 AM   #3
genezing
LQ Newbie
 
Registered: Jun 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
That is not really what I want, I want to use a variable depth, in your code the depth is 3, but I want a script where I can pass for example the depth as $1.
 
Old 06-09-2016, 06:30 AM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,372
Blog Entries: 3

Rep: Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184
Could you define your own bash function so that it can call itself recursively to create directories up to a certain depth?
 
Old 06-09-2016, 06:39 AM   #5
genezing
LQ Newbie
 
Registered: Jun 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
Well I don't know, But if so it would look like something like this I guess:

Code:
recursivedir() {

for (( g=1; g<=$1; g++ ))

($2 somewhere here = depth)
make -p dir
recursivedir(xxxx)
make-p dir

}
 
Old 06-09-2016, 06:49 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,844

Rep: Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478
Just look at it differently... It is nothing but a two dimensional array, with dimensions of size, depth.

You are just sequentially indexing all elements in the array - so using an i,j index, two nested loops would visit each directory... The number given to the given directory is just i + j * size.

The name of the directory is just a counter used to count the creation of a directory...

No recursion required.

Last edited by jpollard; 06-09-2016 at 06:52 AM.
 
Old 06-09-2016, 06:51 AM   #7
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,372
Blog Entries: 3

Rep: Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184
Here is a basic recursion function.

Code:
a () { 
        echo "$1"; 
        if [[ $1 -lt $2 ]]; then 
                b=$(($1+1)); 
                a $b $2; 
        else 
                return; 
        fi 
}
Run it like this:

Code:
a 1 3
Edit: the array approach makes more sense

Last edited by Turbocapitalist; 06-09-2016 at 06:52 AM.
 
Old 06-09-2016, 06:57 AM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,844

Rep: Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478
No recursion is necessary.

Think of it as a two dimensional array, and that you are examining each (i,j) identified element. The name of the directory could just be a counter accumulating the number of elements visited. If you get clever, you can construct the path of directory names you want, then just use the -p option of mkdir to create the entire string (hint - use 1 offset index computations to the i,j conversion).

It actually sounds like a homework assignment...

Last edited by jpollard; 06-09-2016 at 07:01 AM.
 
Old 06-09-2016, 08:15 AM   #9
genezing
LQ Newbie
 
Registered: Jun 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you for your answers but I don't understand it fully, first of all this is not homework, yes I am a student studying Apllied Informatics but in school we don't see any bash scripting unfortunately, this is for my own goal of learning bash and Linux. So you are saying I should use a 2d array but how am I going to make directory on each level with the 2d array?

Code:
size=2
depth=3

for ((i=1;i<=depth;i++)) do
    for ((j=1;j<=size;j++)) do
        mkdir -p --how do I put the depth here?--
    done
done
 
Old 06-09-2016, 08:24 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Revised my function, deleted the original script by accident...:/
Code:
# args: depth size
function makeDirs() {
  depth=$1
  size=$2
  #prevSize=0
  index=0
  lastSize=$2
  
  declare -a 'dirs=(dir{'1..$2'})'
  
  while ((depth-- > 1)); do
    declare -a tmp=(${dirs[@]:$index})
    for d in ${tmp[@]}; do
      for ((i = 0; i < size; i++)); do
        dirs+=($d/dir$((++lastSize)))
      done
      ((index++))
    done
    #((prevSize += size))
  done
  
  mkdir "${dirs[@]}"
}

makeDirs 2 2
Edit, found a bug in logic, changed (and commented) prevSize with index

Last edited by keefaz; 06-09-2016 at 12:59 PM. Reason: script revision 2.0
 
1 members found this post helpful.
Old 06-09-2016, 08:25 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,500

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
mkdir -p can create not only a single dir, but also its parent (recursively) - see man mkdir.
So you only need to generate the list of subdirs (of the given depth) and mkdir -p <list> will do it in one.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help with a recursive bash script yimmy1890 Linux - Newbie 14 03-04-2014 03:43 PM
Elegant bash script needed to make new directories based on matched patterns emmalg Linux - General 4 03-11-2013 06:24 AM
Problem with recursive bash script me4linux Programming 6 03-20-2007 06:46 AM
bash: make rename script traverse directories morrolan Programming 2 11-08-2006 11:52 AM
Help with a recursive chmod script in bash lowpro2k3 Programming 11 07-25-2005 08:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration