-1

On my CentOS machine, I need to create a main directory inside which I will have a few sub directories and, inside them, some subsubdirectories.

Something like:

main_directory->sub1,sub2,sb3..
sub1->subsub1,subsub2,subsub3..
sub2->subsub1,subsub2,subsub3..
sub3->subsub1,subsub2,subsub3..

I want to create this kind of directory structure using a loop and using mkdir inside the loop. Also, I want the user to input all these directories and sub directories and sub sub directory names. How can I do this?

  • Can you be a bit more specific in terms of what you're looking for? How many levels of subdirectories do you want? Given a sample input, explain what the script would produce. – Andy Dalton May 27 '16 at 13:54
  • 2
    How would the user enter the names? How many levels deep do you need this? How will the user specify which directory is under which? Why would you want to do this in a loop? What have you tried so far? Which part of this is giving you trouble? Please edit your question, explain your objective in more detail and show us what you have tried and how it failed. – terdon May 27 '16 at 13:58
  • Thank you Andy for the quick response. there would be one main directory and then there would be few sub directories.i want user to input the number of sub directories he wants to create. then he would enter the names of the sub directories. the when he enters the name of the 1st sub directory, i would want him to enter the number of sub sub directories he wants in the 1st sub directories and so on. then he would enter the names of the sub sub directories. i hope it makes sense. if not then kindly tell me and i will try to explain again in a different manner. – Aditya Agarwal May 27 '16 at 14:01
  • i need it 3 levels deep. il will try to explain this with an example. – Aditya Agarwal May 27 '16 at 14:03
  • 2
    wouldn't it be a lot less tedious for the user for you to just sit them at the bash prompt and tell them how mkdir -p works? an explanation of file globbing and brace expansion would be good too. then they could run something like mkdir -p main/sub{1,2,3}/subsub{1,2,3} – cas May 27 '16 at 15:12
1

It is not limited to a fixed number of dirs, so if you want to create 2, 3 or pass your entire life creating directories and sub-directories, until causing a explosion, here is the script:

#!/bin/bash
enter_recursive(){
while true; do
        echo "Please enter the name of the directory you want to create inside $PWD or type _up to exit the directory"
        read dir
        [ "$dir" = "_up" ] && return
        mkdir "$dir"
        echo -n "Do you want to create subdirectories in $PWD/${dir}? (y/n)"
        read -n1 yn
        echo
        if [ "$yn" == "y" ]; then
                cd "$dir"
                enter_recursive
                cd ..
        fi

done
}

enter_recursive
  • Thank You @Luciano Andress Martini. this is exactly what i needed. thanks a lot!!! – Aditya Agarwal May 27 '16 at 15:13
  • Thank you too! I wrote this script just for your question. By curiosity how this can be usefully? – Luciano Andress Martini May 27 '16 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.