0

I want to create a directory structure like this in nix:

mkdir -p 1,2,3,4,5,6,7,8,9 and within each of these folders I want folders 1,2,3,4,5,6,7,8,9

I have started to write a simple loop like this (all the way up to folder 2) but this seems inefficient.

#!/usr/bin/env bash   
for i in 1 2 4 5 6 7 8 9; do mkdir -p 1/{1,2,3,4,5,6,7,8,9} $i, mkdir -p  2/{1,2,3,4,5,6,7,8,9} $2; done

Is there a better way of doing it?

3 Answers 3

7

This should help (requires bash):

mkdir -p {1,2,3,4,5,6,7,8,9}/{1,2,3,4,5,6,7,8,9}

Some newer versions of bash also allow this:

mkdir -p {1..9}/{1..9}
Sign up to request clarification or add additional context in comments.

3 Comments

3.0-alpha from 2004 introduced brace expansions.
Out of interest, how do I go one step further and add a,b,c,d under each folder. e.g. mkdir -p {1,2,3,4,5,6,7,8,9,a,b,c,d}/{1,2,3,4,5,6,7,8,9,a,b,c,d} with brace expansions?
@user1283693 Yes, it's just brace expansion, so this variant will work with any names, not only numbers.
0

Sounds simple enough unless I've misunderstood:

#!/bin/sh
for i in `seq 1 9`; do
  for j in `seq 1 9`; do
    mkdir -p $i/$j
  done
done

1 Comment

Oh, Michal's one-liner is cooler. I keep forgetting about bash's {} expansion.
0

Perl Solution.

for($counter = 1; $counter <= 9; $counter++)
{
        `mkdir -p $counter/{1..9}`; //Executing Unix Command
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.