Skip to content
Advertisement

Bash Create Directories/Subdirectories Using Two Lists

I would like to create a number of directories, each with a number of subdirectories based on two lists using Bash. Currently, the below code achieves this result:

 mkdir -p {P01DH,P02DL,P03NH,P04NL,P05RH,P06RL}/{folder1,folder2,folder3,folder4,folder5,folder6,folder7,folder8}

Yields

P01DH/folder1
P01DH/folder2 
...
P06RL/folder8

Question: Is there a cleaner (non-single-line) way to achieve the same result, perhaps storing the two lists as arrays or variables and using loops?

Advertisement

Answer

Use nested loops:

for parent in P01DH P02DL P03NH P04NL P05RH P06RL; do
    for child in folder{1..8}; do
        mkdir -p "$parent/$child"
    done
done
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement