user10336264
user10336264

Reputation:

Creating several subdirectories using a for loop

I have a directory whose name is 1990 and I would like to create 12 subdirectories within it (one per month) using a for loop. Also, I want each subdirectory to be named after a month (that is, January, February, March..., instead of 1, 2, 3...). I also don't know which way I should specify the name of the directory (i.e., 1990) in my function.

This is what I was thinking:

for i in 1990; do
    mkdir {January..December}
done

My problems here have to do with the directory itself and with the fact that brace expansion wouldn't accept names.

Note: if there was a possibility of creating the directory 1990 within this function, that would be ok as well.

Upvotes: 1

Views: 227

Answers (1)

glenn jackman
glenn jackman

Reputation: 247250

for m in {1..12}; do date -d "1990-$m-01" "+%Y/%B"; done | xargs mkdir -p

Brace expansion with ranges only works for integers. You'd have to list all the month names:

mkdir -p 1990/{January,February,March,April,May,June,July,August,September,October,November,December}

You could also get date to spit out the commands and pipe into a shell:

for m in {1..12}; do date -d "1990-$m-01" "+mkdir -p %Y/%B"; done | sh

Upvotes: 1

Related Questions