0

I want to create subdirectories into each of directories below. I used the following loop:

#! bash

/# dir m3z m3t m3t2 m3g  m3g2 m3g3 ... n dir        
for d in m3z m3t m3t2 m3g  m3g2 m3g3 ... n dir    
do
    echo "Processing $d"    
    mkdir log #subdir
    mkdir lib #subdir 
    mkdir txt #subdir

done

but it creates one subdirectory at the same level of directories:

$ ls     
m3z m3t m3t2 m3g  m3g2 m3g3 log lib txt .... n dir

I want to get them in this way

$ ls /m3z    
/log /lib /txt

$ ls /m3t
/log /lib /txt
.
.
.

$ ls /n dir
/log /lib /txt

Help?

1

1 Answer 1

7

Assuming the m3* directories exist,

for dir in m3z m3t m3t2 m3g m3g2 m3g3; do
    mkdir "$dir"/{log,lib,txt}
done

or, with brace expansion on the list of directories,

mkdir {m3z,m3t,m3t2,m3g,m3g2,m3g3}/{log,lib,txt}

or even,

mkdir m3{z,t,t2,g,g2,g3}/{log,lib,txt}

or,

mkdir m3{z,t{,2},g{,2,3}}/{log,lib,txt}

or, without the brace expansion,

for dir in m3z m3t m3t2 m3g m3g2 m3g3; do
    mkdir "$dir"/log "$dir"/lib "$dir"/txt
done

or, if the directories that you loop don't already exist,

for dir in m3z m3t m3t2 m3g m3g2 m3g3; do
    mkdir -p "$dir"/log "$dir"/lib "$dir"/txt
done

or, if the directories that you loop over already exist and all match the pattern m3*/,

for dir in m3*/; do
    mkdir "$dir"/log "$dir"/lib "$dir"/txt
done

The main thing is to specify that you'd like to create the directories as subdirectories of $dir, the directory name that you currently process in your loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .