linux - 如何分配目录?

标签 linux bash shell command-line ubuntu

假设我有一个目录列表:

archive_1
archive_2
archive_a
...

有没有一种简单的方法可以将这些目录分布到指定数量的目录中?例如:

distribute -t10 archive_* 

应该生成 10 个目录:sub_1、sub_2、... sub_10 并且每个目录包含total number of archive_* directories/10。类似于 split 的工作方式,但用于目录而不是文件。有什么建议吗?

最佳答案

我认为没有用于此的 Unix 命令,但您可以使用像这样的简单 Python 脚本。要分发目录中的所有文件,请调用 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

关于linux - 如何分配目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7549876/

相关文章:

bash:在文件夹中以 '.' 开头的文件循环时摆脱 '..' 和 '.'

c# - 非 Windows 操作系统上的 .NET 框架 System.Windows.Form

linux - iptables 对每个 ip 地址的限制

linux - 在目录中查找文件夹,而不列出父目录

运行 MATLAB 的 Bash 脚本错误

linux - Shell 脚本 - SFTP -> 如果复制,删除吗?

linux - 如何匹配两个目录中部分匹配的文件名并对找到的文件执行命令

linux - 在Apache中找不到GCC

linux - 无法在 RedHat 6.8 VM 上安装 REALMD

bash - 使用 ssh 运行远程脚本文件时如何使用空格保留参数?