Create Directory structure

Hello ; ) again

Now I have my file like this :

DIR2/DIR3
DIR2
DIR2/DIR3/DIR4/DIR5

I am looking for help to create a loop that will create the directory structure.

I need something like this :

If "DIR2" does not exist > Create 
IF "DIR2" exist already > check if onther "DIR" needs to be created
IF not > GO NEXT STEP of the script
otherwise
create "DIR3" and so on  

Thanks for your help ....

You can use

mkdir -p DIR2/DIR3/DIR4/DIR5

.
or else you can loop around this command for all the input.

You dont need to check the directory and create the directory.
-p option create dir not exist else do nothing.
Then do your operations.
This is just a hint.

Cheers,
Ranga:)

1 Like

Thanks a lot Ranga for your help,

I will try your hint in my script ... thank you so much ...I will let you know ; )

Hello,

Just for you to know the command

mkdir -p

is working great.

But I need help to create a function with a condition. Here is what I am trying to do :

ControleDir () {
for directory in `cat ${fic_cpt_arbo}` ;
do
    mkdir -p ${nfs_depot}/${Directory}
done

But I need to add two condition in my function that will do :

If the directories were create, so continue with message like : "OK" ... If not exit with a warning like "ERROR.

Thanks for your help !

There's a useless use of cat in your code, and variablenames are case-sensitive. For the messages you can use the && and || operators (tested with bash-shell).

ControleDir () {
   while read directory
   do
      mkdir -p ${nfs_depot}/${directory} && echo "OK" || (echo "ERROR"; exit 1)
   done <${fic_cpt_arbo}
}
1 Like

Cero, thanks for your help.