• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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

0
tyleradam
Asked:
tyleradam
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now