Directory Structure Generator

This shell script prompts the user to choose between creating directories or text files. It first asks for the number of main directories to create and a base name for them. Using a loop, it generates these directories with sequential numbering, such as 1_name, 2_name, and so on. After that, the script asks how many subdirectories or text files should be created inside each main directory and what their base name should be. It then loops through all created directories and, based on the user’s initial choice, either creates text files (name1.txt, name2.txt, etc.) or subdirectories (1_name1.dir, 2_name2.dir, etc.). This allows for structured directory and file creation based on user input.

#!/usr/bin/env bash
echo "Click 1 for dir 2 for txt"
read opt
echo "Enter the number of main dir"
read number
echo "Enter name for main dir"
read namee
for i in $(seq 1 $number);
do
        mkdir "$i_$namee"
done
echo "Enter the number of sub dir or txt required:"
read num
echo "Enter name for sub dir"
read name
for dir in $(find . -mindepth 1 -maxdepth 1 -type d);
do
    if [ "$opt" -eq 2 ];then

        for j in $(seq 1 $num);
        do
            touch "$dir/$name$j.txt"
        done

    else 
        for j in $(seq 1 $num);
        do
            mkdir "$dir/$j_$name$j.dir"
        done
    fi

done

This can be used for creating multiple files in the system with text. It can make organizing easy. It can also be used to store information in a secret location where the user can save a text file hidden within many directories. I will make more blogs showing the code for the different types of trees and their uses later.

Hope you learnt something new

Happy Coding…