Bash script to make directories recursive with depth and size
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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
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.
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...
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).
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
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 11:59 AM.
Reason: script revision 2.0
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.