I need some sort of script (shell?) to auto create 365 directories for each day of the year and sub-dirs...

Hello.  I need to create a directory for each day of the year and within that directory create 24 sub dirs for each hour.  I'm guessing some sort of shell script could do this?  But, I'm not very familiar with shell scripting.  The directory structure looks like this:

060101
060101/00
060101/01
060101/02
060101/03
060101/05
060101/06
060101/07
060101/08
060101/09
060101/10
060101/11
060101/12
060101/13
060101/14
060101/15
060101/16
060101/17
060101/18
060101/19
060101/20
060101/21
060101/22
060101/23
060102
060102/00
060102/01
060102/02
060102/03
...etc

I also need all the dirs to be chmod 777.  And owned by a user called wwwuser, Im guessing just running the shell script as that user would do that?  Any help is greatly appreciated on this matter!~!! :D

tyleradamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ravenplCommented:
will do?
for year in "06"; do
 for mon in `seq -f "%02g" 1 12`; do
   for day in `seq -f "%02g" 1 31; do
     mkdir -p "$year$mon$day";
     chmod 0777 "$year$mon$day";
     for hour in `seq -f "%02g" 0 23`; do
       mkdir -p "$year$mon$day/$hour";
       chmod 0777 "$year$mon$day/$hour";
     done
   done
 done
done
0
Jon_FergusonCommented:
This would work but it assumes every month has 31 days.

BASE=/base/path
for month in 1-12; do
if [ $month < 10 ]; then
month=0${month}
fi
for day in 1-31;do
if [ $day < 10 ]; then
day=0${day}
fi
for hour in 0-23; do
if [ $hour < 10 ]; then
hour=0${hour}
fi
if [ ! -d ${BASE}/06${month}${day}/${hour} ]; then
mkdir -p ${BASE}/06${month}${day}/${hour}
fi
done
done
done
chown wwwuser:wwwuser -R ${BASE}/*
chmod 0777 -R ${BASE}/*
0
tyleradamAuthor Commented:
this is what i get when i try Jon's

" ./makedirs
./makedirs: line 3: 10: No such file or directory
./makedirs: line 7: 10: No such file or directory
./makedirs: line 11: 10: No such file or directory
mkdir: cannot create directory `/base': Permission denied
chown: cannot access `/base/path/*': No such file or directory
chmod: cannot access `/base/path/*': No such file or directory
"
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

periwinkleCommented:
:)  You have to change /base/path to the directory under which you want the directories created :)
0
tyleradamAuthor Commented:
ooops. yah i forgot to change that :/

im still getting an error though.

"sh-3.00$ ./makedirs2
./makedirs2: line 4: 10: No such file or directory
./makedirs2: line 8: 10: No such file or directory
./makedirs2: line 12: 10: No such file or directory"

i put #!/bin/sh at the top of the script.  is that incorrect?
0
tyleradamAuthor Commented:
And ravenpl's script gives me this error:

sh-3.00$ ./makedirs
./makedirs: line 7: unexpected EOF while looking for matching ``'
./makedirs: line 14: syntax error: unexpected end of file
0
periwinkleCommented:
It's running, so the "shebang" line is correct - that's fine.

Does the base directory exist?  What the script is saying is that you are trying to make a directory underneath a directory structure that doesn't exist.
0
tyleradamAuthor Commented:
Yah, I have BASE set as
BASE=/home/wwwuser/test

Which exists.
0
ravenplCommented:
for mon in `seq -f "%02g" 1 12`; do
   for day in `seq -f "%02g" 1 31`; do
     mkdir -p "06$mon$day";
     chmod 0777 "$year$mon$day";
     chown wwwuser: "$year$mon$day";
     for hour in `seq -f "%02g" 0 23`; do
       mkdir -p "$year$mon$day/$hour";
       chmod 0777 "$year$mon$day/$hour";
       chown wwwuser: "$year$mon$day/$hour";
     done
   done
 done
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
periwinkleCommented:
Yes, I think ravenpl is correct - use the seq;  I was able to see your same problem when using the earlier scripts.  Ravenpl's script has a small error in it (but please do credit him!) - the variable $year isn't set.  If you replace $year with 06, you should be fine.

After seeing that the other construct wasn't working locally, I was able to get working a simplified version using:

#!/bin/sh
BASE=/home/misha/testbin
for month in `seq -f "%02g" 1 12`; do
for day in `seq -f "%02g" 1 31`; do
for hour in `seq -f "%02g" 0 23`; do
echo ${BASE}/06${month}${day}/${hour}
if [ ! -d ${BASE}/06${month}${day}/${hour} ]; then
mkdir -p ${BASE}/06${month}${day}/${hour}
fi
done
done
done

The echo command simply tells you where you are;  you can omit that line if you don't want a long slew of information.

Ravenpl's solution is even more elegant with the chown and chmod commands - my vote is points to Ravenpl!
0
tyleradamAuthor Commented:
Awesome!  That works great.  Thanks a lot for getting me straightened out :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.