Creating new directories

I want to create a new system of directory structure for example

mkdir -p ./iv.sac/resu/hhz.d

However, I think that `mkdir -p` overwrites the directories. I want to avoid that
and am doing something as follows in my bash script

echo -e "\nCreating directories:" 
odir_nwk="./iv.sac/resu/hhz.d"
if [ -d "$odir_nwk" ]; then
  echo "Directory already exists: $odir_nwk"
else
  echo "mkdir -p $odir_nwk"
fi

I suppose this will only check if the directory `./iv.sac/resu/hhz.d`
exists but might overwrite `./iv.sac/resu` for example.

Would like to make this foolproof. What can I do?

What do you mean by "overwrite"?

If ./iv.sac/resu/hhz.d exists, so does ./iv.sac/resu and won't be touched.

man mkdir :

mkdir -p will create intermediary directories only as necessary. That is, it won't try to re-create it if the directory already exists.

1 Like

Many directories will be created and I want to avoid a situation where
if there is a mistake the user might loose any directories or files.

I don't really follow you. Nothing will be lost with 'mkdir', despite your earlier assertion that "I think that `mkdir -p` overwrites the directories. I want to avoid that". mkdir doesn't do anything except make directiories. It doesn't delete anything.

It's a Unix thing - 'do one thing, and do it well' :wink:

1 Like

Not an assertion, just a clarification. Sometime there might be some option or whatever to delete things. But as you mention it's ok.

It is always a good idea to make use of the man pages on the system you are using those unix commands.