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?
RudiC
February 17, 2018, 2:44pm
2
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
:
�-p�
�--parents�
Make any missing parent directories for each argument, setting their file permission bits to the umask modified by �u+wx�. Ignore existing parent directories, and do not change their file permission bits.
milhan
February 17, 2018, 2:52pm
3
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.
Scott
February 17, 2018, 5:59pm
5
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'
1 Like
scott:
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'
Not an assertion, just a clarification. Sometime there might be some option or whatever to delete things. But as you mention it's ok.
milhan
February 18, 2018, 7:39am
7
It is always a good idea to make use of the man pages on the system you are using those unix commands.