LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Better way to create a folder for each month, with another folder inside of each (https://www.linuxquestions.org/questions/linux-newbie-8/better-way-to-create-a-folder-for-each-month-with-another-folder-inside-of-each-4175439809/)

anon091 12-03-2012 08:51 AM

Better way to create a folder for each month, with another folder inside of each
 
I was going to do this manually, but figure it might be a good question to ask, as i'm sure there is a much slicker way to do it.

What I want to do is inside my directory /2012 I want to make a folder for each month of the year, like 01-2012, 02-2012, etc. then inside each ##-2012 folder, I want to make a folder named processed.

Last year I manually did mkdir's for each one, but is there some way to better create these?

markush 12-03-2012 09:13 AM

Code:

for i in {01..12}; do mkdir -p $i-2012/processed; done
probably you mean such a oneliner?

Markus

anon091 12-03-2012 09:16 AM

Wow, that worked great, except that it didn't do 01, 02; it did just 1, 2, etc. Is there a way to make it do the leading zero for even the single digit months?

markush 12-03-2012 09:19 AM

Which shell are you using? here I have (with Bash 4.2.37)
Code:

markus@samsung:~/2012$ ls
01-2012/  02-2012/  03-2012/  04-2012/  05-2012/  06-2012/  07-2012/  08-2012/  09-2012/  10-2012/  11-2012/  12-2012/

did you really type {01..12} for the sequence?

Markus

druuna 12-03-2012 09:27 AM

BashFAQ/018 - leading zeros in a loop

Includes: Bash 3, Bash 4, POSIX shell, ksh and BSD style examples.

anon091 12-03-2012 09:27 AM

bash 3, its an older box.

I copied and pasted what you typed, so I had the 01

anon091 12-03-2012 09:34 AM

Thanks druuna, I did a for i in 0{1..9} and a for i in {10..12} and that did it. I wasn't sure how to combine them.
Thanks to you too Markus.

druuna 12-03-2012 09:43 AM

Quote:

Originally Posted by rjo98 (Post 4841790)
I did a for i in 0{1..9} and a for i in {10..12} and that did it.

It can be done in one for loop:
Code:

for i in {0{1..9},10,11,12}; do mkdir -p $i-2012/processed; done

anon091 12-03-2012 09:45 AM

Ah cool, thanks. I wasn't sure how to combine them. Wasn't sure if you should do a 10..12 or like how you did it.

GazL 12-03-2012 10:38 AM

In bash 4.2 you can forgo the for loop and just use:
Code:

mkdir -p {01..12}-2012/processed

And just to show that there is always another way to do everything in UNIX ;)
Code:

seq -w 1 12 | xargs -i -- mkdir -p {}-2012/processed
... which will avoid any shell inconsistencies and also involve less process setup/teardown than a for loop (though that really won't be noticeable for just 12 entries).


All times are GMT -5. The time now is 01:21 AM.