7

I am trying to figure out an elegant way to create a new subdirectory in a series of existing directories.

I have 15 already created directories called Week1 through Week15. Within those directories, I'd like to create a new directory called "Assignments." I'm thinking this might require a loop of some kind, but outside of that I'm a little lost.

I know I can "cd" into each directory and then "mkdir Assignment" and repeat this step for all 15 directories, but I know there must be an easier way.

Any help/advice will be greatly appreciated!

0

2 Answers 2

13

You can do it without a loop:

mkdir -p Week{1..15}/Assignments
3
  • 5
    Note that the -p option specifies to make the parent dirs as necessary, so this will work even if you haven't created Week1 - Week15 yet. Sep 18, 2014 at 17:22
  • 2
    This is called brace expansion, in case someone was wondering.
    – Alaa Ali
    Sep 19, 2014 at 2:18
  • 2
    mkdir -p Week{01..15}/Assignments
    – alfC
    Sep 19, 2014 at 2:47
4

There are a few ways - try using this loop:

for i in {1..15}; do mkdir Week$i/Assignments; done

You could also cheat at the Week folders - use this:

for i in {1..15}; do mkdir Week$i; done

Just for future reference - if you need to do something similar.

You must log in to answer this question.

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