We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

tyleradam
tyleradam asked
on
Medium Priority
453 Views
Last Modified: 2010-04-20
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

Comment
Watch Question

Top Expert 2005

Commented:
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
Top Expert 2006

Commented:
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}/*

Author

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
"
CERTIFIED EXPERT

Commented:
:)  You have to change /base/path to the directory under which you want the directories created :)

Author

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?

Author

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

Commented:
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.

Author

Commented:
Yah, I have BASE set as
BASE=/home/wwwuser/test

Which exists.
Top Expert 2005
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Commented:
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!

Author

Commented:
Awesome!  That works great.  Thanks a lot for getting me straightened out :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.