开发者

How to distribute directories?

Let us say I have a list of directories:

archive_1
archive_2
archive_a
...

Is there a simple way to distribute these directories into a specified number of directories? For instance something like:

distribute -t10 archive_* 

should produce 10 directories: sub_1, sub_2, ... sub_10 and contain total number of archive_* directories/10 in each. Something like how split works but for directories instead开发者_Python百科 of files. Any suggestions?


I don't think there is a Unix command for this, but you can use a simple Python script like this. To distribute all files in a directory, invoke as distribute.py -c10 -p sub *

#!/usr/bin/python

import sys, os, shutil
from optparse import OptionParser

p = OptionParser()
p.add_option("-c", "--count", type="int", default=10,
    help="Number of dirs to distribute into", metavar="NUM")
p.add_option("-p", "--prefix", type="string", default="sub",
    help="Directory prefix", metavar="PREFIX")

(options, args) = p.parse_args()

for x in range(0, options.count):
    os.mkdir("%s_%d" % (options.prefix, x))

c = 0
for f in args:
    shutil.move(f, "%s_%d" % (options.prefix, c))
    c += 1
    c %= options.count


#!/usr/bin/ksh

dirs=$(ls ${3})
for i in dirs
do
cd $i
for j in 1..$2
do
mkdir sub_$j
done
cd ..
done

execute it this way:

distribute -t 10 archive_  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜