Stephanie
Stephanie

Reputation: 9

Bash file that creates a directory and subdirectories

I'm new to Bash and have a little program that asks for an input between 1 and 10 and then proceeds to create the same amount of directories the user typed. At the moment my program creates all of the directories in one location, so I'm wondering if there's a way I can create a main folder and then the subdirectories inside one another.

For example:

How many directories would you like to create?

user input: 3

folder1    
      |-folder2    
              |-folder3

Any tips on how to get my program to do this will be very much appreciated.

#!/bin/bash
read -p "How many directories would you like?" num_folder
if test $num_folder -lt 10
then
    for ((i=0; i<num_folder; i++)); do
      mkdir folder$i        
    done

    tree -c

    read -rsp "Press enter to continue"
    clear

else
    echo "Please write a number between 1 and 10"      
fi

Upvotes: 0

Views: 71

Answers (2)

Geno Chen
Geno Chen

Reputation: 5214

Another solution maybe the usage of -p parameter with mkdir.

mkdir -p "$(seq -f "folder%0.0f" -s "/" $num_folder)"

Upvotes: 2

Gery
Gery

Reputation: 9056

how about:

seq 1 3 | xargs mkdir

you can simply use numbers.

Upvotes: 0

Related Questions