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

I need a shell script to create multiple sub dirs based on a number

Hey guys.  I need a shell script to create a TON of directories.  I'm trying to break down the images on my social networking site and figure I would do it by their ID number.  For example... members #4567 would have all their files in:

/4/5/6/7/foo.jpg

Member #39
/3/9

etc

So I need a shell script for Linux that can automatically create all these directories.  From 1 to 1,000,000.  Anyone know how to do this?
0
tyleradam
Asked:
tyleradam
  • 7
  • 4
  • 3
2 Solutions
 
ozoCommented:
perl -MFile::Path -e 'mkpath[join"/",split//] for 1..1000000'
0
 
TintinCommented:
#!bin/bash
for i in `seq 1 1000000`
do
   mkdir -p /`echo $i|sed 's#\(.\)#\1/#g'`
done
0
 
tyleradamAuthor Commented:
I'm kind of a linux newb.  When I run tintin's script, I get:

bin/bash: bad interpreter: No such file or directory
0
Industry Leaders: 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!

 
ozoCommented:
try
#!/bin/bash
0
 
tyleradamAuthor Commented:
Isn't that what's in his?

Just to let you guys know what I did:


vi doit
-entered code-
chmod 777 doit
./doit
0
 
ozoCommented:
try
#!/bin/bash
instead of
#!bin/bash
or run
bash ./doit
0
 
tyleradamAuthor Commented:
Looks to be working!  Thanks guys.  I had to put a . before the /`echo $i|sed  part... it created all those dirs in my root when it finally worked hehe.

0
 
tyleradamAuthor Commented:
Shoot.  It worked when I set the limit to 1000... but when I set it to 1M... it doesnt do anything.  Do you guys know why this would be?
0
 
TintinCommented:
Put an echo in, eg:

#!/bin/bash
for i in `seq 1 1000000`
do
   echo $i
   mkdir -p ./`echo $i|sed 's#\(.\)#\1/#g'`
done
0
 
ozoCommented:
Could it just be taking a long time?
0
 
tyleradamAuthor Commented:
I guess it's just taking a long time.  I commented out the mkdir command and just left the echo $i command for testing.  I set the limit to 100.  It echo'd instantly.   I set it to 1,000 and it was instant.  I set it to 10,000 and it paused for a moment and then began to scroll the numbers.  I then set it to 100,000 and it paused... I waited like an hour and still nothing.  I had to go somewhere.. I came back later that day and it had printed.  The end looked like:

999997
999998
999999
1e+06

So, I figure a million will take days!  Might there be a faster way?  It looks as if it's counting first... then taking action.  Is there a way to do it as you go?
0
 
TintinCommented:
Try ozo's solution or

#!/bin/bash
i=1
while [ $i -le 1000000 ]
do
  echo $i
  mkdir -p ./`echo $i|sed 's#\(.\)#\1/#g'`
  let i=i+1
done
0
 
tyleradamAuthor Commented:
Thanks guys!  That one worked a bit better.  All is good now :D
0
 
tyleradamAuthor Commented:
Hey guys.  I have a follow up question.. if you want to attempt it!  :D

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Q_22894420.html
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now