LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-03-2012, 09:51 AM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Rep: Reputation: 47
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?
 
Old 12-03-2012, 10:13 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

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

Markus
 
1 members found this post helpful.
Old 12-03-2012, 10:16 AM   #3
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
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?
 
Old 12-03-2012, 10:19 AM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
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
 
Old 12-03-2012, 10:27 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
BashFAQ/018 - leading zeros in a loop

Includes: Bash 3, Bash 4, POSIX shell, ksh and BSD style examples.
 
2 members found this post helpful.
Old 12-03-2012, 10:27 AM   #6
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
bash 3, its an older box.

I copied and pasted what you typed, so I had the 01
 
Old 12-03-2012, 10:34 AM   #7
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
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.
 
Old 12-03-2012, 10:43 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by rjo98 View Post
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
 
1 members found this post helpful.
Old 12-03-2012, 10:45 AM   #9
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
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.
 
Old 12-03-2012, 11:38 AM   #10
GazL
Senior Member
 
Registered: May 2008
Posts: 4,188
Blog Entries: 5

Rep: Reputation: 1623Reputation: 1623Reputation: 1623Reputation: 1623Reputation: 1623Reputation: 1623Reputation: 1623Reputation: 1623Reputation: 1623Reputation: 1623Reputation: 1623
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).
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] create sub-folder from folder name and transfer files on the fly BoydRice Linux - General 5 03-07-2012 08:44 PM
Cant create a folder inside samba share (join in AD2003) micro_xii Linux - Newbie 3 12-07-2011 09:25 PM
How to create a "fixed-space"-a folder that is EXT2 inside and whatever size-50MB? linus72 Linux - Newbie 3 04-08-2009 09:47 AM
create a folder/file within a folder using perl indu.a Programming 2 03-07-2007 11:09 PM
how can i default the max folder file size when it create inside a folder antony_csf Linux - Software 1 06-17-2004 03:26 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration