LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-10-2007, 01:47 PM   #1
iandroul
Member
 
Registered: Feb 2007
Posts: 32

Rep: Reputation: 15
create many directories simultaneously


Hi

I want to create 360 empty directories that each one of them has a date of the year\
e.g. 1st directory's title: 010103
2nd directory's title: 010203
etc

How can I do that without doing it one by one?


Thanks
 
Old 12-10-2007, 02:13 PM   #2
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Code:
#!/bin/bash

MAXDIR=360
line=0

while [ "$line" -le $MAXDIR ]
do
mkdir -p 01"$line"03
((line+=1))
done
 
Old 12-10-2007, 02:41 PM   #3
iandroul
Member
 
Registered: Feb 2007
Posts: 32

Original Poster
Rep: Reputation: 15
thanks

how can i add another statement inside :

while [ "$line" -le $MAXDIR ]
do
mkdir -p 01"$line"03
((line+=1))
done


for example:


while [ "$line" -lt 10]

mkdir -p 010"$line"03
((line+=1))
done

#and if "$line" -ge 10 and -le $MAXDIR ]
#
#do
#mkdir -p 01"$line"03
#((line+=1))
#done


"the difference is the 0 before the month character"
 
Old 12-10-2007, 02:42 PM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
My crude attempt:
Code:
#!/bin/bash

YEAR_START=3
YEAR_END=32

for YR in $(seq $YEAR_START $YEAR_END); do
   if [ $YR -lt 10 ]; then
      for MONTH in $(seq 1 12); do
         if [ $MONTH -lt 10 ]; then
            mkdir 010"$MONTH"0"$YR"
         else
            mkdir 01"$MONTH"0"$YR"
         fi
      done
   else
      for MONTH in $(seq 1 12); do
         if [ $MONTH -lt 10 ]; then
            mkdir 010"$MONTH""$YR"
         else
            mkdir 01"$MONTH""$YR"
         fi
      done
   fi
done
edit: I've done a bit of reading, and you can tweak the for loops so you end up with this instead:
Code:
#!/bin/bash

YEAR_START=3
YEAR_END=32

for YR in $(seq $YEAR_START $YEAR_END); do
   if [ $YR -lt 10 ]; then
      for MONTH in {1..12}; do
         if [ $MONTH -lt 10 ]; then
            mkdir 010"$MONTH"0"$YR"
         else
            mkdir 01"$MONTH"0"$YR"
         fi
      done
   else
      for MONTH in {1..12}; do
         if [ $MONTH -lt 10 ]; then
            mkdir 010"$MONTH""$YR"
         else
            mkdir 01"$MONTH""$YR"
         fi
      done
   fi
done

Last edited by pwc101; 12-10-2007 at 02:50 PM.
 
Old 12-10-2007, 02:44 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Obviously, if you are going to increment the date, you will need something more elaborate inside the loop. I'm looking for the commands that would--eg--increment the date by 1 day.

EDIT: I type slowly....
I'm thinking there has to be a way to increment date by days, weeks, or months--I'll post if I find it.

Last edited by pixellany; 12-10-2007 at 02:50 PM.
 
Old 12-10-2007, 03:55 PM   #6
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Why 360? Is this supposed to be one directory for each day (there are 366 possible dates)? Should this be MMDDYY or DDMMYY? Do you want to have it follow the months such that months w/ 31 get 013103 (or 310103) and February 022803 (or 280203), etc. or do you simply want 360 directories with 010103 through 046003?

Let us know,

Forrest
 
Old 12-10-2007, 04:10 PM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by forrestt View Post
Should this be MMDDYY or DDMMYY?
Ah yes, I didn't think of that - I just assumed DDMMYY.

More information needed.
 
Old 12-10-2007, 04:23 PM   #8
iandroul
Member
 
Registered: Feb 2007
Posts: 32

Original Poster
Rep: Reputation: 15
Yes you are right

Eventually I used the below script for each month separately (31 days, 28 days and 30 days) to make the 360 directories for 2003 (e.g. DAYMONTHYEAR)\

Thank you all

#!/bin/bash

MAXDIR=10
line=0

for line in {1..31}; do
if [ "$line" -lt $MAXDIR ]; then
mkdir -p 0"$line"1203
((line+=1))
else
mkdir -p "$line"1203
((line+=1))
fi
done

Last edited by iandroul; 12-10-2007 at 04:27 PM.
 
Old 12-10-2007, 04:31 PM   #9
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Do me a favor and run the command "ls *03 |wc -l" in the directory that you made all these directories in and let me know what the output is. I'm still not getting what it is you are trying to do.

Forrest
 
Old 12-10-2007, 05:21 PM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Code:
for ((n=0; n<365; n++)); do 
    mkdir $(date "+%m%d%y" -d "2003-01-01 + $n days")
done
 
Old 12-10-2007, 08:18 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by matthewg42 View Post
Code:
for ((n=0; n<365; n++)); do 
    mkdir $(date "+%m%d%y" -d "2003-01-01 + $n days")
done
I suspected there was something like this, but I could not find it in the man pages, "info date", ABS, or Google.

In particular, where do you find docs on the ability to do " + $n days"?
 
Old 12-10-2007, 08:26 PM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by pixellany View Post
I suspected there was something like this, but I could not find it in the man pages, "info date", ABS, or Google.

In particular, where do you find docs on the ability to do " + $n days"?
It's a good question. I have known this for a while, and I think I must have read it in a manual page at some point. It's something I have noticed - that the manual pages which ship with many distros seem to be somehow not very complete. In the case of Ubuntu, infotex is also absent, so I often see things like "full documentation is found in the info page", and then I can't view it.
 
Old 12-10-2007, 08:33 PM   #13
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Aha, a quick google told me that the info pages are installed, but you need to type:
Code:
info coreutils date
...to get to the relevant page. The node you seek can be found using this command (assuming you have the info pages installed:
Code:
info coreutils date "Relative items in date strings"
 
Old 12-10-2007, 11:20 PM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by matthewg42 View Post
Aha, a quick google told me that the info pages are installed, but you need to type:
Code:
info coreutils date
...to get to the relevant page. The node you seek can be found using this command (assuming you have the info pages installed:
Code:
info coreutils date "Relative items in date strings"
Thanks!!!--I obviously was not looking carefully. That is a really powerful command...
 
Old 12-11-2007, 06:58 AM   #15
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
For some reason I have never really liked info pages - I always preferred man pages. Maybe it's that they're all split up into sub-pages I prefer one long document. However, there is a lot of good information in there if you can work out how to navigate through it.

By the way, you can use konqueror as an info browser. Just go type info:/ in the location bar.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't create directories tebucky Linux - Newbie 5 10-21-2006 03:39 PM
Samba: "homes" share, cannot create directories, can create files Herg Linux - Software 1 09-14-2006 08:48 AM
Unable to create directories satimis Linux From Scratch 8 07-02-2005 04:48 AM
Can't create directories in /proc keripukki Linux - Software 3 09-03-2004 01:26 PM
Create several directories? AutOPSY Linux - Newbie 3 03-30-2004 07:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:47 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
Open Source Consulting | Domain Registration